使用 document.write()

使用 document.write() 可能會導致網頁內容延遲數十秒才顯示,對於連線速度較慢的使用者來說尤其成問題。因此在多數情況下,Chrome 會封鎖 document.write() 的執行作業。 因此您無法仰賴它

當您在 Chrome 開發人員工具控制台中使用 document.write() 時,系統會顯示以下訊息:

[Violation] Avoid using document.write().

在 Firefox 開發人員工具控制台中,您會看到以下訊息:

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

Lighthouse document.write() 稽核失敗的原因

Lighthouse 標記 呼叫未遭 Chrome 封鎖的 document.write()

Lighthouse 稽核顯示 document.write 的使用情形

對於大多數問題的用途而言 Chrome 將封鎖對 document.write() 的呼叫 或根據使用者的連線速度,觸發控制台警告。 無論如何,開發人員工具控制台中都會顯示受影響的呼叫。 詳情請參閱 Google 的「針對 document.write() 進行干預」一文。

Lighthouse 回報對 document.write() 的所有剩餘通話 因為無論使用方式如何,都會對成效造成負面影響 但還有更好的替代方案

建議不要使用 document.write()

移除程式碼中所有 document.write() 的使用方式。用途: 插入第三方指令碼,請嘗試使用 非同步載入

如果第三方程式碼使用 document.write(),請要求供應商支援非同步載入功能。

資源