When is document.write
OK?
There are many places where programmers are encouraged to avoid document.write
(even the HTML5 specification gives it a hardy slap), which is a good thing because it's one of those rather clunky things that was introduced at the very start of web scripting that was never standardised or even specified and it has been superceded by other methods.
However, there is still at least one case where it might be considered helpful.
User agents that don't support scripting
Many web pages have hundreds of kb of scripts, mostly because developers just drop in libraries and plugins with no regard for page size because it saves a few hours of development time and developers consider their time more important that that of their client or employer's visitors.
Browsers generally won't download scripts if scripting is disabled or not available, but some may. Using script to insert scripts means that if scripting isn't available, the script elements are never placed in the document and the related resources are never downloaded.
document.write
is a very simple way of implementing a script loader. Sure, there are much more sophisticated script loaders to do the same thing, but good old document.write
is dead basic, works everywhere and, for this purpose, does the job superbly with the same ease as innerHTML.
And given the widespread use of innerHTML (and even markup snippets as a method of creating elements using DOM methods), it seems reasonable to use a similar tool for inserting scripts.
Where insertion using innerHTML doesn't work
This is almost the same as the one above, but a little different.
Script elements inserted using innerHTML aren't executed, so if the document stream is open, it's pretty trivial to use document.write
instead of innerHTML. The usual caveat applies though, using document.write
after the document is loaded will first remove the current document, which is not always (err, almost never) desirable.
Popup windows
Ok, everyone hates pop–ups and combining them with document.write
seems like the worst of the worst of the worst. But sometimes a simple pop–up with content written by document.write
is simpler and faster (both to develop and present) than more sophisticated dialogues.
The XHTML excuse
document.write
doesn't work in non–HTML documents (e.g. XML). But while many pages on the web have an XHTML DOCTYPE (possibly because CMSs prefer XML to HTML), the pages are almost always served as text/HTML, so that's how the browser treats them. It's extremely unlikely that the web will move to XML (i.e. documents actually served as XML) anytime soon. For web pages, the DOCTYPE is essentially a flag used by the browser to work out if it should be in standards mode or not, so the XML thing is a bit of a Phurphy.
However, the bottom line is that document.write
should almost never be used "in the real world" because DOM methods provide a standardised alternative, have well specified behaviour and are almost universally supported. document.write
is more or less equivalent to eval
in that there are some rare cases where it's useful, but there is nearly always a better way to do things.