为什么document.write被认为是“不良做法”? [英] Why is document.write considered a "bad practice"?

查看:110
本文介绍了为什么document.write被认为是“不良做法”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 document.write 被视为不良做法;我希望编制一份提交给第三方供应商的理由清单,说明为什么他们不应该在他们的分析代码的实现中使用 document.write 。 / p>

请在下面提供您声明 document.write 的理由。

解决方案

一些比较严重的问题:




  • document.write(以下称为DW)在XHTML中不起作用


  • DW不直接修改DOM,阻止进一步操作 (试图找到这方面的证据,但它最好情况化)


  • 页面加载完成后执行的DW会覆盖页面,或者写一个新页面,或者不工作


  • DW在遇到的地方执行:它不能在给定的节点点注入


  • DW正在有效地编写序列化文本,这不是DOM在概念上的工作方式,而且是一种简单的方法创建错误(.innerHTML有同样的问题)




更好地使用安全和DOM友好 DOM操作方法


I know document.write is considered bad practice; and I'm hoping to compile a list of reasons to submit to a 3rd party vendor as to why they shouldn't use document.write in implementations of their analytics code.

Please include your reason for claiming document.write as a bad practice below.

解决方案

A few of the more serious problems:

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

  • DW does not directly modify the DOM, preventing further manipulation (trying to find evidence of this, but it's at best situational)

  • 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

  • DW is effectively writing serialised text which is not the way the DOM works conceptually, and is an easy way to create bugs (.innerHTML has the same problem)

Far better to use the safe and DOM friendly DOM manipulation methods

这篇关于为什么document.write被认为是“不良做法”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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