document.write()造成什么损害? [英] What damage is done by document.write()?

查看:167
本文介绍了document.write()造成什么损害?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前有什么不好的事情发生在 document.write()被调用?

What bad things happen at the moment document.write() is invoked?

我听说过关于 document.write 的一些不利影响对DOM或使用Javascript库有不利影响。我在我面前有一个问题,我怀疑是相关的,但是还没有找到一个简明扼要的总结,说明该方法有什么损害。

I've heard bits and pieces about document.write having an adverse impact on the DOM or on the use of Javascript libraries. I have an issue in front of me that I suspect is related, but have not been able to find a concise summary of what damage the method does.

推荐答案

使用 document.write()将破坏网页 - 破坏并覆盖整个DOM - 如果被调用该文件已被完成解析。这被认为是对 document.write()的使用不畅,并且是批评许多旧脚本的原因。

Use of document.write() will break a web page - destroying and overwriting the entire DOM - if it's called after the document has finished being parsed. This is considered a poor use of document.write() and is/was the reason for criticism of a lot of older scripts.

window.onload = function ()
{
    document.write("Oops!");
}

一般来说,这是可以接受的,在解析时更广泛地用于添加内容以同步方式动态地页面:

Generally though, it's acceptable and rather widely used at parse-time to add something dynamically to the page in a synchronous manner:

<div>
  <script type="text/javascript">
  document.write("Well I'll be, your browser supports JavaScript!");
  </script>
</div>

广告发布服务主要用于将广告添加到网页,一些Google API也使用。

It's mostly used by ad publishing services for adding the advertisements to a page, some Google APIs also use it.

这篇关于document.write()造成什么损害?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆