When JavaScript was first created by Netscape they needed a way for the scripts to be able to interact with the web page. The way that was chosen was an extremely simple approach. The document.write statement simply outputs content into the page just as if that content were a part of the HTML in the first place. At that time there was not much choice regarding server side processing of the content before displaying the page and so any way of adding content into the page dynamically was a huge step forward.

As anything could be added to the page from JavaScript which could affect how the rest of the web page would display, the rendering of the rest of the web page needs to be halted while the JavaScript runs so that any document.write statements in the script can output their content prior to attempting to render whatever follows. Also the script needed to be included in the exact spot in the page where the document.write was to output its content and to run immediately at that point since running after the page had finished loading would result in the document.write statements overwriting the page completely.

Now there are a large range of server side scripts to choose from that can dynamically create the HTML on the server. There are very few things that JavaScript has access to that these server side scripts don't have access to and so in the vast majority of cases where document.write used to be useful the task can now be performed more efficiently on the server. Once server side languages were created that could easily work directly with the HTML had been created, the situations where document.write is useful was reduced to those situations where what gets output is dependent on information about the browser itself.

The limitations on how you could update web pages from JavaScript disappeared when the Document Object Model was introduced to allow the page content to be dynamically changed after it has finished loading. This far more flexible way of updating the page from JavaScript (whether using the official DOM commands or more recent additions such as innerHTML) effectively made the document.write statement obsolete (except when used to dynamically generate entire web pages).No longer did the JavaScript have to be scattered through the JavaScript and could in fact be all grouped together at the bottom of the page (where it wouldn't delay the rendering of the page). For those rare cases where the final content was dependent on browser settings default content could be output in the HTML and then updated by JavaScript running afterwards. Other changes to the page could also be made from JavaScript without needing to reload the entire page from the server.

Of course there is still a lot of old web pages written back before the more advanced ways of interacting with the page was introduced and it took some time for those sites to update their code to use the more advanced and less obtrusive ways of performing those interactions (many smaller sites still have not done so). Perhaps the most significant change in this regard was when Google finally updated their ad scripts to use a more modern approach.

The only web pages you find around now that still use document.write are those that were written long ago and never updated and those written by people who have copied antiquated JavaScript code into their page rather than learning how to write it properly. Any web pages in either of these groups are likely to have other sites competing with them where the scripts have been modernised to remove the antiquated document.write statements.

This raises the question of whether or not our browser really needs to support this antiquated command any more. Just what would happen if we told the browser to ignore any document.write statements and to continue outputting the HTML without adding anything from the write statement. For all the web sites using properly written modern JavaScript, disabling the document.write command would have no impact whatsoever. Only those web sites still using the antiquated document.write statement would be affected and the only effect on these antiquated web pages would be to leave out that part of the content added by the document.write statement. You'd see these older web pages as if you were visiting them with JavaScript turned off so far as that output is concerned but any other JavaScript in the page would still be able to run. As you can't create web applications using document.write, it is reasonable to expect that these web pages should still work with JavaScript completely disabled and so in almost all cases they should also work with just document.write disabled. Only pages that are really poorly designed might break if the document.write statement were disabled (and they'd likely not work without JavaScript either) and in those cases you'd most likely want to move on to the next web site anyway.

Turning off document.write in your browser should therefore be possible without it having any noticeable effect on your browser use except for making it easier to identify some of the worst designed pages where the page is likely broken in other ways anyway. I think it is therefore reasonable to say that we could turn off this command in our browser (whether we should or would is a different matter).


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow