Skip to content

document.write()

A legacy JavaScript API that writes content directly into the HTML stream during parsing. Disables Chrome's preload scanner on slow connections; major performance anti-pattern.

document.write() is a JavaScript API from the early web that injects content into the HTML stream while it's being parsed. Modern web development considers it an anti-pattern for several reasons:

Preload-scanner kill switch. Chrome (and most modern browsers) run a preload scanner ahead of the HTML parser to discover and start fetching CSS / JS / image resources in parallel. When the parser hits document.write(, the preload scanner has to stop -- it can't predict what the write call will produce, so it can't pre-fetch anything past it. On 2G / slow 3G connections, Chrome explicitly disables the preload scanner the moment it detects document.write, serializing the entire subtree's loading.

Re-flow cost. Writing into a partially-parsed document forces the browser to discard parsing state and start over from the injection point. On large documents this can add hundreds of ms to render time.

Race-condition trap. If you call document.write() AFTER the page finishes loading (e.g., from an event handler), it overwrites the entire document, blanking the screen. Real bug, surprisingly common.

Where it still appears. Almost exclusively in legacy ad-tag code (Google Ads' SafeFrame is the canonical modern alternative; many older ad networks haven't migrated) and ancient analytics snippets. If you're integrating a vendor and they hand you a script that uses document.write, push back -- it's a strong signal they haven't updated their integration in 5+ years.

Modern equivalents: document.createElement + appendChild for DOM injection; <template> elements for client-side templating; MutationObserver for reactive content insertion. Chrome's DevTools Performance panel explicitly flags document.write usage in the audits.

Related terms

Further reading

Send Feedback