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.
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
- Open each flagged tag and identify what
document.write()is injecting — typically a<script>tag or an HTML block. - For script injection: create the element with
document.createElement('script'), set itssrcortextContent, and append it withdocument.head.appendChild()ordocument.body.appendChild(). - For HTML injection: assign to
innerHTMLon a target element, or useinsertAdjacentHTML()for positional control. - Test in GTM Preview mode to confirm the replaced logic loads correctly.
- Publish once verified.
Metadata
- Severity
- high
- Tier
- optimization
- Category
- performance
- Layer
- container
- Version
- 1
- Effort
2hr(about 2 hours)- Reversibility
- non-destructive
See also
- Performance rules — other rules in this category
- Health score — how this rule contributes to the score
- Rules catalog — browse all rules
Examples
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"}'
User: Run document-write on https://example.com
# Tool call
{
"tool": "evaluate_rule",
"arguments": {
"rule_id": "document-write",
"url": "https://example.com"
}
}