< HTML>
< BODY>
< P>Hello World!< /P>
< DIV> < P>The DOM is very useful!< /P>
< P>This example demonstrates the < B>document.body< /B> property.< /P>
< /DIV>
< SCRIPT TYPE="text/Javascript">
x=document.body;
alert(x.innerHTML);
document.write(x.innerHTML);
< /SCRIPT>
< /BODY>
< /HTML>
I basically got this from an internet tutorial...The original version only has the alert(x.innerHTML) and I added in document.write(x.innerHTML).
Original version is stable in the sense that its output is always the same but after my addition, the output is something like a loop but I am not sure why.
Can anyone here tell me what was the thing that I missed?
x.innerHTML would contain everything between the <body> tags (see below), so by calling document.write(x.innerHTML), you're also re-inserting the JS code into the web document.
x.innerHTML:
< P>Hello World!< /P>
< DIV> < P>The DOM is very useful!< /P>
< P>This example demonstrates the < B>document.body< /B> property.< /P>
< /DIV>
< SCRIPT TYPE="text/Javascript">
x=document.body;
alert(x.innerHTML);
document.write(x.innerHTML);
< /SCRIPT>
As you can see, this would result in the browser parsing the same JS code again, resulting in a loop.
If you wish to display the code on the web page, you'll need to convert the special characters (such as < and >) to HTML entities so that the browser would recognize them as text rather than HTML code and parse them. You may wish to use the following JS function for this purpose:
http://phpjs.org/functions/htmlspecialchars:426
Thanks for the explanation...
managed to use character codes to convert the () in document.write...
document.write&40;x.innerHTML&41;;
works in the sense that my display stops its infinite loop and displays correctly in the document but the alert() will contain the special characters for the document.write.
if I change alert(x.innerHTML); to alert&40;x.innerHTML&41;;
the alert will not appear..
any way to easily keep the alert(x.innerHTML) but change the information parsed into document.write into special characters?
Why do you need the document.write statement in the first place?
The modified line "document.write&40;x.innerHTML&41;;", while valid as a JS statement, doesn't do anything other than to return integer values because the ampersands are recognized as bitwise ANDs and the semicolons simply close the statement that precedes them.
Originally posted by LatecomerX:Why do you need the document.write statement in the first place?
The modified line "document.write&40;x.innerHTML&41;;", while valid as a JS statement, doesn't do anything other than to return integer values because the ampersands are recognized as bitwise ANDs and the semicolons simply close the statement that precedes them.
its more for understanding the difference in parsing different input parameters and how to manipulate them as parse inputs.Right now the sitation is that I either have an loop or have an alert with special characters when I play around with the special characters so I am missing a link in how to parse inputs which are codes by themselves.
What I trying to do is for the document to show the coding in the correct format, likewise for the alert.
http://lx.sg/null/20100303.html
Press Ctrl+U to view source code.
And I still don't understand what you're trying to achieve here.