470,591 Members | 1,586 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

innerHTML of another frame

I have a page as follows:

<HTML>
<FRAMESET ROWS="20%, 80%">
<FRAME SRC="editor.html" NAME="editor">
<FRAME SRC="testfile.html" NAME="reader">
</FRAMESET>
</HTML>

editor.html contains the following code:

code = document.documentElement.innerHTML

which gives me the HTML-code of editor.html as expected.
Now I want to read the HTML-code of testfile.html instead. But

code = parent.reader.document.documentElement.innerHTML

just returns

<head></head><body></body>

with Mozilla, and IE gives an error.
Can somebody explain why this happens, and how I can solve it?
Since the first case works on all browsers I tried, I was hoping to get
a browser-independant method to read the sourcecode of another file.
By the way, all files are local.

Thanks

Jul 20 '05 #1
1 13637
Christian Schmitt <cs******@cip.physik.uni-wuerzburg.de> writes:
I have a page as follows:

<HTML>
<FRAMESET ROWS="20%, 80%">
<FRAME SRC="editor.html" NAME="editor">
<FRAME SRC="testfile.html" NAME="reader">
</FRAMESET>
</HTML>

editor.html contains the following code:

code = document.documentElement.innerHTML
Where is it in editor.html? Inside a function or just directly inside
a script tag. I.e., when is it executed?
which gives me the HTML-code of editor.html as expected.
Now I want to read the HTML-code of testfile.html instead. But

code = parent.reader.document.documentElement.innerHTML
I prefer to use the frames collection:
var code = parent.frames['reader'].document...
I have still to see a browser where it is required, though.
just returns

<head></head><body></body>

with Mozilla, and IE gives an error. Can somebody explain why this happens, and how I can solve it?
Most likely cause: The testfile.html page is not loaded yet at the
time when you exexute the code.

Solving it: Wait.
Preferably until you are sure both pages have loaded. You can use the
onload handler on the frameset to do that.

Example:

<frameset onload="window.LOADED = true;">

and in editor.html:

<script type="text/javascript">
var code = null;
function testCode() {
if (parent.LOADED) {
code = parent.frames['reader'].document.documentElement.innerHTML;
} else {
setTimeout(testCode,500);
}
}
testCode();
</script>

Then don't use the code variable before it has been initialized.
Since the first case works on all browsers I tried, I was hoping to get
a browser-independant method to read the sourcecode of another file.
You didn't try Netscape 4 or Opera 6, or any other browser not implementing
the "innerHTML" property. It is not part of any standard, so depending on it
might be dangerous. It is implemented in many modern browsers, though. Mostly
for compatability with pages written for IE.
By the way, all files are local.


As long as they are from the same domain, it should work.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by JimMenees | last post: by
4 posts views Thread by Nick | last post: by
1 post views Thread by jhcorey | last post: by
1 post views Thread by hapyfishrmn | last post: by
5 posts views Thread by SAM | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.