使用 document.write()

Published on Updated on

Translated to: English, Español, Português, 한국어

使用 document.write() 会导致页面内容显示延迟数十秒,对于网络连接速度较慢的用户来说,这更是一个问题。因此,在许多情况下,Chrome 会阻止执行 document.write(),这就意味着您不能依赖这种方法。

在 Chrome DevTools 控制台中使用 document.write() 时,您会看到以下消息:

[Violation] Avoid using document.write().

在 Firefox DevTools 控制台中,您会看到以下消息:

An unbalanced tree was written using document.write() causing
data from the network to be reparsed.

Lighthouse document.write() 审核为何失败

Lighthouse 对未被 Chrome 阻止的 document.write() 的调用:

显示 document.write 使用的 Lighthouse 审核

对于最容易产生问题的用途,Chrome 将阻止对 document.write() 的调用或发出相关的控制台警告,具体取决于用户的连接速度。无论采用哪种方式,受影响的调用都会显示在 DevTools 控制台中。有关详细信息,请参阅 Google 的干预 document.write()

Lighthouse 会报告对 document.write() 的任何其他调用,因为无论如何使用,它都会对性能产生不利影响,并且我们有更好的替代方法。

Each Best Practices audit is weighted equally in the Lighthouse Best Practices Score. Learn more in The Best Practices score.

避免使用 document.write()

删除代码中对 document.write() 的所有使用。如果已将其注入第三方脚,请尝试使用异步加载

如果第三方代码正在使用 document.write(),请要求提供程序支持异步加载。

资源

Updated on 改进文章

This site uses cookies to deliver and enhance the quality of its services and to analyze traffic. If you agree, cookies are also used to serve advertising and to personalize the content and advertisements that you see. Learn more about our use of cookies.