7

I've seen a bit of chatter about document.write being deprecated, but I'm not sure exactly where this information came from. I did look it up in MDN, but there wasn't any notation about it being deprecated.. so now I'm a bit suspicious. Google wasn't much help either unfortunately (perhaps I just wasn't using the right search terms).

If it is indeed deprecated, can someone link me to the appropriate documents showing that it is indeed deprecated?

Stephen
  • 5,098
  • 1
  • 18
  • 33
  • 4
    It's been [functionally superceded by more appropriate means of manipulating content](http://dev.w3.org/html5/spec/content-models.html#content-models), thus "deprecated" is more a state of mind than a reality. It's a relic, it may never go away, but most of it's use is no longer the best practice. – Jared Farrish Sep 24 '12 at 23:11
  • Nice wording @JaredFarrish, I couldn't put it better. – Fabrício Matté Sep 24 '12 at 23:12
  • Note, the W3C reference: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-75233634 – Jared Farrish Sep 24 '12 at 23:14
  • And the HTML5 spec: http://dev.w3.org/html5/spec/dynamic-markup-insertion.html#dynamic-markup-insertion – Jared Farrish Sep 24 '12 at 23:16
  • An interesting treatise on [exploiting the advice that's hard to prove](http://me.veekun.com/blog/2012/03/24/on-principle/). – Jared Farrish Sep 24 '12 at 23:22
  • The `whatever gets the job done is good enough` from the linked page above is very true to many organizations nowadays, sadly. – Fabrício Matté Sep 24 '12 at 23:25
  • Heh, thanks folks - this is why I never really trust anything I read on the net without some sort of primary source to back it up. – Stephen Sep 24 '12 at 23:32
  • Little off-topic, but now that I linked that question in my answer, the OP of that question was compiling a list of reasons to submit to an analytics vendor.. Now it made me wonder if that's somehow related to the Google Analytics' synchronous `document.write` method. Edit: Reading through the second most upvoted answer there, yes that's quite clear. – Fabrício Matté Sep 24 '12 at 23:36

3 Answers3

10

No. It's just most often considered bad practice and almost as misused as eval.

Read: Why is document.write considered a 'bad practice'?

Quoting some important points from the linked question above:

  • document.write (henceforth DW) does not work in XHTML

  • DW executed after the page has finished loading will overwrite the page, or write a new page, or not work

  • DW executes where encountered: it cannot inject at a given node point

Also as @JaredFarrish stated, deprecated is mostly a state of mind. It's a relic that most likely will never go away otherwise it'd break many sites - even the Traditional Google Analytics code uses DW.

Obviously, functionality-wise it has been superseded long ago by proper DOM manipulation methods, and quoting the linked question above again: DW is effectively writing serialised text which is not the way the DOM works conceptually.


To counterbalance, here's a list of where DW may be considered appropriate:

  • There's nothing wrong with DW, per se - it won't destroy your page if you use it appropriately;
  • It allows for easily fallbacking an external script to a local copy such as when loading jQuery from a CDN fails:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/libs/jquery-1.8.2.min.js">\x3C/script>');</script>
    
  • It's one of the easiest ways to insert external snippets in your page while keeping the code short (e.g. analytics). Note that systems such as Google Analytics now favor the asynchronous method - creating a script element through the document.createElement API, setting its properties and appending it to the DOM - rather than using its traditional document.write method.

tl;dr:
DW is easily misused, hence for real world use, prefer a proper DOM manipulation method whenever viable.

Community
  • 1
  • 1
Fabrício Matté
  • 65,581
  • 23
  • 120
  • 159
  • +1, but perhaps you could provide some bullet points as to why its bad practice from the article? – Jeffrey Sweeney Sep 24 '12 at 23:07
  • Seems everyone loves lists of why not to use it. In the interests of a balanced discussion, how about a list of when it is appropriate? – RobG Sep 25 '12 at 00:45
  • @RobG Fair enough. `appropriate` is a rather subjective view though. DW is most often a way to get the job done the quick and dirty way - similarly to `.innerHTML`. You see w3schools widely using DW for many pages' examples, which is frowned upon by w3fools. I sincerely can't think of anything that couldn't be easily replaced by proper DOM manip methods besides the external JS fallback. Even the GA has an asynchronous method now which is (subjectively) more appropriate. – Fabrício Matté Sep 25 '12 at 00:57
3

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.

Community
  • 1
  • 1
RobG
  • 124,520
  • 28
  • 153
  • 188
  • `Popup windows` is a decent excuse, however any library or very small snippets will provide dialog modals with optional templating which IMO provide much better usability than popups which may happen to be blocked by the users' browsers, forcing the developer to request the user to unblock popups to enable some of the page's functionality. – Fabrício Matté Sep 25 '12 at 01:55
  • Oh never mind about the "User agents that don't support scripting", I had to read it a 3rd time. Users with JS-disabled are still a vast minority. I never actually considered whether scripts would be downloaded with JS disabled, but I assume most browsers would simply ignore script elements in this case. It'd be nice to know some of the modern browsers which do download these, though. – Fabrício Matté Sep 25 '12 at 02:06
  • The question was whether it is deprecated. I don't see any answer to that question in this post. – Ingo Bürk Oct 06 '13 at 00:09
  • @IngoBürk—You're right, because Fabricio already answered that. – RobG Oct 07 '13 at 06:51
0

In the W3C HTML5 specification [W3C Recommendation 28 October 2014 section 6.3.3] document.write() still exists, https://www.w3.org/TR/html5/webappapis.html#document.write() although there is a rather explicit warning in the specification, ending with... "For all these reasons, use of this method is strongly discouraged."

garwoodpr
  • 11
  • 3