Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

javascript - 为什么document.write被视为“不良做法”?(Why is document.write considered a “bad practice”?)

I know document.write is considered bad practice;

(我知道document.write被认为是不良做法;)

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.

(并且,我希望整理出一份为什么要向第三方供应商提交的理由清单,说明他们为什么不应该在其分析代码的实现中使用document.write 。)

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

(请在下面document.write您主张document.write的不良做法。)

  ask by FlySwat translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A few of the more serious problems:

(一些较严重的问题:)

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

    (document.write(此后称为DW)在XHTML中不起作用)

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

    ( DW不会直接修改DOM,从而阻止了进一步的操作 (试图寻找证据,但充其量只是视情况而定))

  • 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在遇到的地方执行:它无法在给定的节点上注入)

  • 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)

    (DW有效地编写了序列化的文本,这不是DOM在概念上的工作方式,并且是创建错误的简便方法(.innerHTML存在相同的问题))

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

(使用安全且对DOM友好的DOM操作方法要好得多)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...