Is document.write really outdated?

I saw a bit of chatter about document.write , which is deprecated, but I don’t know exactly where this information came from. I looked at it in MDN , but there was no sign in which this would be obsolete .. so now I'm a little suspicious. Google didn't help much unfortunately (maybe I just didn't use the right search terms).

If this is really out of date, can someone link me to the relevant documents showing that it is really out of date?

+6
source share
3 answers

Not. This is most often considered bad practice and is almost as misused as eval .

Read: Why is document.write considered "bad practice"?

Highlighting some important questions from a related question above:

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

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

  • DW makes a counter call: it cannot insert node at a given point

Just as @JaredFarrish stated, deprecated is basically a state of mind. This is a relic that is likely to never disappear, otherwise it will break many sites - even Google Analytics’s Traditional Code uses DW.

Obviously, functionally wise, he was replaced long ago by the correct DOM manipulation methods and again quoted the related question: DW is effectively writing serialised text which is not the way the DOM works conceptually .


To balance, here is a list of where the DW can be considered appropriate:

  • There is nothing wrong with DW in itself - it will not destroy your page if you use it appropriately;
  • This makes it easy to get rid of an external script to a local copy, for example when loading jQuery from a CDN:

     <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> 
  • This is one of the easiest ways to insert external snippets on your page while saving a short code (e.g. analytics). Note that systems such as Google Analytics now prefer the asynchronous method - creating a script element through the document.createElement API, setting its properties and adding it to the DOM - instead of using its traditional document.write method.

TL; dr:
DW is easy to abuse, so they prefer a suitable method of manipulating the DOM for use in the real world whenever they are viable.

+10
source

When is document.write OK?

There are many places where programmers are advised to avoid document.write (even the HTML5 specification gives it a hard slap in the face), which is good because it is one of those rather clumsy things that were introduced at the very beginning of web scripts that were never standardized or were not even indicated and were replaced by other methods.

However, there is at least one other case where it can be considered useful.

User agents that do not support scripting

Many web pages have hundreds of kb scripts, mainly because developers simply drop libraries and plugins, not paying attention to page size, because it saves several hours on development, and developers consider their time more important than the time of their client or visitors - employers.

Browsers will usually not load scripts if scripts are disabled or unavailable, but some may. Using a script to insert scripts means that if scripts are not available, script elements are never placed in the document, and related resources are never loaded.

document.write is a very simple way to implement a script loader. Of course, there are much more complex script loaders to do the same, but the good old document.write is dead, works everywhere and for this purpose does the job perfectly with the same ease as innerHTML.

And given the widespread use of innerHTML (and even markup fragments as a method of creating elements using DOM methods), it seems reasonable to use a similar tool to insert scripts.

If insert using innerHTML does not work

This is almost the same as above, but slightly different.

Script elements inserted with innerHTML are not executed, so if the document stream is open, it is quite simple to use document.write instead of innerHTML. The usual caveat applies, however, using document.write after loading the document, first delete the current document, which is not always (preferably, almost never) desirable.

Popup windows

Well, everyone hates pop-ups and combining them with document.write seems the worst of the worst of the worst. But sometimes a simple pop-up menu with document.write text is simpler and faster (both for development and presentation) than more complex dialogs.

XHTML Apology

document.write does not work in documents other than HTML (e.g. XML). But while many pages on the Internet have XHTML DOCTYPE (perhaps because CMSs prefer XML over HTML), the pages almost always serve as text / HTML, so the browser views them. It is highly unlikely that in the near future the web will move to XML (that is, documents actually used as XML). For web pages, DOCTYPE is essentially the flag used by the browser for development if it should be in standard mode or not, so the XML thing is a bit of Phurphy.

However, the bottom line is that document.write almost never be used "in the real world," because the DOM methods provide a standardized alternative, have well-defined behavior, and are almost universally supported. document.write more or less equivalent to eval in that there are some rare cases where this is useful, but there is almost always a better way to do something.

+3
source

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

0
source

Source: https://habr.com/ru/post/926162/


All Articles