What are all the flaws of document.write

I know that there are many problems using document.write , and I avoid this absolutely. However, I had a problem with third-party widgets that use it. I don’t quite know how to put it in words (and also probably don’t know all the reasons) why this little piece of pure evil should be avoided.

I have already rewritten the code that the third party provides to the embedded widget so that it does not use document.write . However, this code loads another script that uses it. I am delaying the loading of a script that calls document.write calls after onload , rewriting my entire page.

So the question is, what are all the problems with using document.write , so that I can provide an exhaustive list of reasons why a third party should correct their code?

Thank you in advance!

+4
source share
3 answers

Quote from the following entry :

  • Since the built-in scripts make the user's browser wait until the code is complete (even if we have to wait for the external URL to load) before rendering the rest of the page.

  • Since we cannot continue the process (modify / duplicate, etc.), exit before embedding it in the actual web page.

  • They also inflate xhtml code
+4
source

My only experience with document.write is where it fills the rest of the page, so I would avoid that.

In addition, with libraries like jQuery, as well as using JavaScript selector functions, the contents of the elements can be changed instead of making sure document.write placed where you want its contents. This means that you can keep your JavaScript separate from your markup, making everything a lot cleaner and more manageable.

+1
source

I would like to add to Darin’s answer a note about using document.write after the page has finished loading. The document flow closes after the page has finished loading. This is done by the browser. If you try to use document.write after this happens, the document stream opens again and accepts input, but does not close automatically. This puts the browser in a state in which it still believes that it is downloading content. If you notice an endless “connection” to the server, this may be a possible reason. Other side effects may be a blank page (not sure if this is in IE or FireFox).

+1
source

All Articles