PDA

View Full Version : document.write() method



uk1simon
Jan 18th, 2010, 02:55 PM
Hello,

I'm just trying to understand what JavaScript Bible says about the document.write() method and it confuses me a bit. Have a look:


Bear in mind that after a page loads, the browser’s output stream is automatically
closed. After that, any document.write() method issued to the current page
opens a new stream that immediately erases the current page (along with any variables
or other values in the original document). Therefore, if you wish to replace
the current page with script-generated HTML, you need to accumulate that HTML
in a variable and perform the writing with just one document.write() method.
You don’t have to explicitly clear a document and open a new data stream; one
document.write() call does it all.

"with just one statement" this really keeps nagging me.

And then a few lines down:


Also, any document.write() method invoked
later will only append to the page, rather than clear the existing content to write
anew.

So in my undestanding you can use a few document.write() methods and then document.close() or you can accumulate html code in a variable and use just one document.write() method and the result is the same.

although accumulating html in a variable and using it as a parameter of the document.write() method saves a lot of typing.


So what's your take on that?



Regards,
Simon

Old Pedant
Jan 18th, 2010, 08:17 PM
Hmmm...I guess the first question is simple: Why would you ever *WANT* to replace the current page contents using document.write()??

I can see creating new content. I can see creating new content *OR* replacement content in another frame or window. But I can't see replacing the current page content. If you do that, then even the JavaScript code you are using to do the response.write() gets wiped.

Having said all the above...<grin/>...yep. Works the same. You want a demo??


<html>
<body>
<script>
function zap()
{
document.write('<html><body>'
+ '<a href="javascript:alert(document.body.innerHTML);">click</a>\n'
+ '<br/>Prior content is now gone.');
for ( var i = 1; i < 6; ++i ) document.write("<br/>Extra stuff #" + i );
document.write('</body></html>');
document.close(); // not really needed
}
</script>
<a href="javascript:alert(document.body.innerHTML);">show me</a>
<a href="javascript:zap();"> ZAP! </a>
</body>
</html>

rnd me
Jan 18th, 2010, 10:39 PM
So in my undestanding you can use a few document.write() methods and then document.close() or you can accumulate html code in a variable and use just one document.write() method and the result is the same.
Regards,
Simon

you can only use document.write() before the page loads.
yes, it's probably faster to build a string rather whan repeat calls to document.write, but that can also cause problems, like when collecting script definitions for example. If i meant to write a script tag that loads a function that a later part of my pre-page-load script uses, it will fail.


I generally use .innerHTML instead of document.write.

document.write is great for adding script tags that load before the page finishes, commenting out some contents under certain conditions, injecting off-site remote data into a specific place in the document without server proxies, or for creating popups with dynamic content.

uk1simon
Jan 19th, 2010, 08:54 PM
cheers guys,



regards,
Simon

godofreality
Jan 19th, 2010, 09:32 PM
only other reason i could ever see for using document.write would be for a nonjavascript enabled version of the site and a javascript version of the site since if javascript is enabled u could wipe out the nonenabled version and replace it with the enabled version

Old Pedant
Jan 19th, 2010, 11:33 PM
Yuck.

Grasping at straws.

Use style.display="none" if you really need something like that.