开发者

What are all the evils of document.write [closed]

开发者 https://www.devze.com 2023-04-03 19:59 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

I know there are lots of issues with using document.write and I avoid it absolutely. However, I have run into an issue with a 3rd party widget that is using it. I don't quite know how to put into words (and also probably don't know all the reasons) why this little slice of pure evil should be avo开发者_如何学Cided.

I've already rewritten the code the 3rd party gives to inline their widget so that it doesn't use document.write. However, that code loads a further script that uses it. I'm delaying the loading of the script which is causing its document.write calls to happen after onload, overwriting my entire page.

So the question is, what are all the issues with using document.write so that I can provide a comprehensive list on why the 3rd party should fix their code?

Thank you in advance!


Quote from the following post:

  • Because inline scripts force user’s browser to wait for the code to complete (even if we have to wait for an external url to load) before rendering the rest of page.

  • Because we can’t further process (modify/duplicate etc) the output before embedding it in the actual webpage.

  • Also they bloat the xhtml code


My only experience with document.write is one where it blanks the rest of the page, so for that reason I'd avoid it.

Also, with libraries like jQuery as well as native JavaScript selector functions, the content of elements can be changed instead of having to make sure document.write is placed wherever you want the content. This means you can keep your JavaScript separate from your markup, making everything a lot cleaner and more manageable.


I'd like to add to Darin's answer with a note about using document.write after the page has finished loading. The document stream is closed once the page has finished loading. This is done by the browser. If you attempt to use document.write after this has happened, the document stream is opened again and accepts input, but isn't automatically closed. This puts the browser in a state where it still thinks it is loading content. If you notice a never ending "connection" to the server, this could be a possible reason. Other side effects could be a blank page (not sure if this is in IE or FireFox).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号