为什么 document.write 被认为是“不好的做法"? [英] Why is document.write considered a "bad practice"?
问题描述
我知道document.write
被认为是不好的做法;并且我希望编制一份理由清单,向第三方供应商提交关于为什么他们不应该在他们的分析代码的实现中使用 document.write
的原因.
请在下面说明您将 document.write
声明为不良做法的原因.
Please include your reason for claiming document.write
as a bad practice below.
推荐答案
一些比较严重的问题:
document.write(以下称为 DW)在 XHTML 中不起作用
document.write (henceforth DW) does not work in XHTML
DW 不直接修改 DOM,防止进一步操作 (试图找到这方面的证据,但充其量只是情境)
页面加载完成后执行的DW会覆盖页面,或者写一个新页面,或者不工作
DW executed after the page has finished loading will overwrite the page, or write a new page, or not work
DW 在遇到的地方执行:它不能在给定的节点点注入
DW executes where encountered: it cannot inject at a given node point
DW 正在有效地编写序列化文本,这不是 DOM 概念上的工作方式,并且是一种创建错误的简单方法(.innerHTML 也有同样的问题)
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)
使用安全且对 DOM 友好的 DOM 操作方法要好得多一个>
Far better to use the safe and DOM friendly DOM manipulation methods
这篇关于为什么 document.write 被认为是“不好的做法"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!