Skip to content
Learn Measure Blog Case studies About
On this page
  • How the Lighthouse document.write() audit fails
  • Avoid document.write()
  • Resources

Uses document.write()

May 2, 2019 — Updated Jun 4, 2020
Available in: Español, 한국어, Português, 中文, English
Appears in: Best Practices audits
On this page
  • How the Lighthouse document.write() audit fails
  • Avoid document.write()
  • Resources

Using document.write() can delay the display of page content by tens of seconds and is particularly problematic for users on slow connections. Chrome therefore blocks the execution of document.write() in many cases, meaning you can't rely on it.

In the Chrome DevTools Console you'll see the following message when you use document.write():

[Violation] Avoid using document.write().

In the Firefox DevTools Console you'll see this message:

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

How the Lighthouse document.write() audit fails #

Lighthouse flags calls to document.write() that weren't blocked by Chrome:

Lighthouse audit showing usage of document.write

For the most problematic uses, Chrome will either block calls to document.write() or emit a console warning about them, depending on the user's connection speed. Either way, the affected calls appear in the DevTools Console. See Google's Intervening against document.write() article for more information.

Lighthouse reports any remaining calls to document.write() because it adversely affects performance no matter how it's used, and there are better alternatives.

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

Avoid document.write() #

Remove all uses of document.write() in your code. If it's being used to inject third-party scripts, try using asynchronous loading instead.

If third-party code is using document.write(), ask the provider to support asynchronous loading.

Resources #

  • Source code for Uses document.write() audit
  • Intervening against document.write()
  • Parser blocking versus asynchronous JavaScript
  • Speculative parsing
Last updated: Jun 4, 2020 — Improve article
Return to all articles
Share
subscribe

Contribute

  • File a bug
  • View source

Related content

  • developer.chrome.com
  • Chrome updates
  • Web Fundamentals
  • Case studies
  • Podcasts
  • Shows

Connect

  • Twitter
  • YouTube
  • Google Developers
  • Chrome
  • Firebase
  • Google Cloud Platform
  • All products
  • Terms & Privacy
  • Community Guidelines

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies.