使用 document.write()
— 更新日期
Appears in: 最佳做法审核
使用 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()
的调用:
对于最容易产生问题的用途,Chrome 将阻止对 document.write()
的调用或发出相关的控制台警告,具体取决于用户的连接速度。无论采用哪种方式,受影响的调用都会显示在 DevTools 控制台中。有关详细信息,请参阅 Google 的干预 document.write()
。
Lighthouse 会报告对 document.write()
的任何其他调用,因为无论如何使用,它都会对性能产生不利影响,并且我们有更好的替代方法。
避免使用 document.write()
#
删除代码中对 document.write()
的所有使用。如果已将其注入第三方脚,请尝试使用异步加载。
如果第三方代码正在使用 document.write()
,请要求提供程序支持异步加载。
资源 #
Last updated: — Improve article