Skip to content

Why document.write is slow and how to replace it

Best practices1 min readFixes check no-document-write

document.write blocks parsing and Chrome may ignore it on slow connections. Insert elements with DOM methods instead.

Step by step, with screenshots: Console errors and deprecated APIs: what the report flags →

Check your own site

Runs this check and the other 186, free, in about 45 seconds.

What a passing site looks like

  • document.write() is not usedwarning · −1 ptseffort M

document.write() usage

Why it matters. document.write blocks the page until the injected script arrives, and Chrome drops such scripts on slow connections, so the ad or widget simply does not appear.

How to fix it.
  1. Replace document.write with a normal <script async> tag or by appending the element from JavaScript.
  2. Update the ad or widget snippet; most vendors now provide an async version.

Filed under Best practices. Copy is generated from the same catalogue that scores every report, so what you read here is what the report says.