Skip to main content
TagManifest /Docs Dashboard

document.write() usage in Custom HTML tags

Detects Custom HTML tags using document.write(), which blocks page rendering and can cause blank pages when called after initial load.

On this page 6

Summary

  • rule_id — document-write
  • purpose — Detect Custom HTML tags that call document.write() at runtime
  • effect — Blocks HTML parsing on load; wipes the page when called post-load
  • layer — container
  • reversibility — non-destructive

What fires this

The rule checks all active Custom HTML tags (via getHtmlTags) for the codeAnalysis.hasDocumentWrite flag. The parser sets this flag when it detects a document.write( call in the tag code. Any tag where the flag is true and the tag is not paused is included in the finding. The rule does not evaluate what is being written — all document.write calls are flagged regardless of content.

Why it matters

document.write() writes synchronously to the document stream. Called during initial parse, it blocks HTML parsing until the write completes. Called after the document has loaded, it wipes the entire page and rewrites it from scratch — producing a blank screen. Chrome warns about document.write() in DevTools and blocks it silently on slow connections. GTM fires tags asynchronously after the page begins loading. This means nearly every document.write() call inside a GTM Custom HTML tag executes post-load, guaranteeing the destructive path.

Before / after

// Injects a vendor script via document.write — flagged
document.write('<script src="https://vendor.example/tag.js"><\/script>')
// Async DOM injection — no parser blocking
const s = document.createElement('script')
s.src = 'https://vendor.example/tag.js'
document.head.appendChild(s)

Remediation

  1. Open each flagged tag and identify what document.write() is injecting — typically a <script> tag or an HTML block.
  2. For script injection: create the element with document.createElement('script'), set its src or textContent, and append it with document.head.appendChild() or document.body.appendChild().
  3. For HTML injection: assign to innerHTML on a target element, or use insertAdjacentHTML() for positional control.
  4. Test in GTM Preview mode to confirm the replaced logic loads correctly.
  5. Publish once verified.

Metadata

Severity
high
Tier
optimization
Category
performance
Layer
container
Version
1
Effort
2hr (about 2 hours)
Reversibility
non-destructive

See also

Examples

Evaluate
curl https://developer.tagmanifest.com/v1/evaluate-rule \
  -H "Authorization: Bearer $TM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"rule_id":"document-write","url":"https://example.com"}'