最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is document.write actually deprecated? - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question edited Sep 25, 2012 at 2:00 Stephen asked Sep 24, 2012 at 22:59 StephenStephen 5,4601 gold badge23 silver badges34 bronze badges 8
  • 4 It's been functionally superceded by more appropriate means of manipulating content, 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 Commented Sep 24, 2012 at 23:11
  • Nice wording @JaredFarrish, I couldn't put it better. – Fabrício Matté Commented Sep 24, 2012 at 23:12
  • Note, the W3C reference: w3/TR/DOM-Level-2-HTML/html.html#ID-75233634 – Jared Farrish Commented Sep 24, 2012 at 23:14
  • And the HTML5 spec: dev.w3/html5/spec/… – Jared Farrish Commented Sep 24, 2012 at 23:16
  • An interesting treatise on exploiting the advice that's hard to prove. – Jared Farrish Commented Sep 24, 2012 at 23:22
 |  Show 3 more ments

3 Answers 3

Reset to default 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./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.

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 bining 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.

In the W3C HTML5 specification [W3C Remendation 28 October 2014 section 6.3.3] document.write() still exists, https://www.w3/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."

发布评论

评论列表(0)

  1. 暂无评论