我懂文件。写作被认为是坏习惯;我希望整理一份理由清单,提交给第三方供应商,告诉他们为什么不应该使用文档。编写分析代码的实现。

请附上申领文件的理由。下面是一个坏习惯。


当前回答

Pro:

这是从外部(到您的主机/域)脚本嵌入内联内容的最简单方法。 你可以覆盖一个frame/iframe中的全部内容。在更现代的Ajax技术广泛可用之前(1998-2002),我经常在菜单/导航部分使用这种技术。

Con:

它序列化渲染引擎,直到加载外部脚本,这可能比内部脚本花费更长的时间。 它通常以这样一种方式使用,即将脚本放在内容中,这被认为是糟糕的形式。

其他回答

根据Google-Chrome Dev Tools的Lighthouse Audit所做的分析,

对于连接速度较慢的用户,通过document.write()动态注入的外部脚本可能会将页面加载延迟数十秒。

它使用XML呈现来分割页面(如XHTML页面)。

最好:一些浏览器切换回HTML渲染,一切工作正常。

可能:某些浏览器在XML呈现模式下禁用了document.write()函数。

最糟糕的情况:某些浏览器在使用document.write()函数时会触发一个XML错误。

一些比较严重的问题:

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)

最好使用安全且DOM友好的DOM操作方法

文件的缺点。写作主要取决于这3个因素:

)实现

write()主要用于在需要内容时立即将内容写入屏幕。这意味着它发生在任何地方,要么在JavaScript文件中,要么在HTML文件中的脚本标记中。由于script标记被放置在这样的HTML文件中的任何地方,在与网页中的HTML交织在一起的脚本块中使用document.write()语句是一个坏主意。

b)呈现

Well designed code in general will take any dynamically generated content, store it in memory, keep manipulating it as it passes through the code before it finally gets spit out to the screen. So to reiterate the last point in the preceding section, rendering content in-place may render faster than other content that may be relied upon, but it may not be available to the other code that in turn requires the content to be rendered for processing. To solve this dilemma we need to get rid of the document.write() and implement it the right way.

c)不可能的操作

一旦写好了,就结束了。如果不进入DOM,我们就不能返回去操作它。

可以将document.write()(和. innerhtml)看作是对源代码字符串求值。这对于许多应用程序来说非常方便。例如,如果您从某个源获取HTML代码作为字符串,那么只需“计算”它就很方便。

在Lisp的上下文中,DOM操作就像操作一个列表结构,例如通过执行以下操作创建列表(橙色):

(cons 'orange '())

而document.write()就像求值一个字符串,例如通过这样求值一个源代码字符串来创建一个列表:

(eval-string "(cons 'orange '())")

Lisp还具有使用列表操作创建代码的非常有用的能力(例如使用“DOM样式”创建JS解析树)。这意味着你可以使用“DOM样式”构建一个列表结构,而不是“字符串样式”,然后运行该代码,例如:

(eval '(cons 'orange '()))

如果您实现编码工具,例如简单的实时编辑器,那么能够快速计算字符串是非常方便的,例如使用document.write()或. innerhtml。Lisp在这个意义上是理想的,但是你也可以用JS做一些很酷的事情,很多人都在做,比如http://jsbin.com/