[{"data":1,"prerenderedAt":411},["ShallowReactive",2],{"rule-document-write":3},{"id":4,"title":5,"body":6,"breadcrumb":374,"coderail":381,"dateModified":400,"datePublished":400,"description":401,"extension":402,"meta":403,"navigation":404,"next":400,"path":405,"prev":400,"schemaType":406,"seo":407,"stem":408,"wide":409,"__hash__":410},"docs\u002Frules\u002Fdocument-write.md","document.write() usage in Custom HTML tags",{"type":7,"value":8,"toc":366},"minimark",[9,16,58,63,82,86,97,101,216,220,272,276,334,338,362],[10,11,12],"p",{},[13,14,15],"strong",{},"Summary",[17,18,19,30,40,46,52],"ul",{},[20,21,22,25,26],"li",{},[13,23,24],{},"rule_id"," — ",[27,28,29],"code",{},"document-write",[20,31,32,35,36,39],{},[13,33,34],{},"purpose"," — Detect Custom HTML tags that call ",[27,37,38],{},"document.write()"," at runtime",[20,41,42,45],{},[13,43,44],{},"effect"," — Blocks HTML parsing on load; wipes the page when called post-load",[20,47,48,51],{},[13,49,50],{},"layer"," — container",[20,53,54,57],{},[13,55,56],{},"reversibility"," — non-destructive",[59,60,62],"h2",{"id":61},"what-fires-this","What fires this",[10,64,65,66,69,70,73,74,77,78,81],{},"The rule checks all active Custom HTML tags (via ",[27,67,68],{},"getHtmlTags",") for the ",[27,71,72],{},"codeAnalysis.hasDocumentWrite"," flag. The parser sets this flag when it detects a ",[27,75,76],{},"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 ",[27,79,80],{},"document.write"," calls are flagged regardless of content.",[59,83,85],{"id":84},"why-it-matters","Why it matters",[10,87,88,90,91,93,94,96],{},[27,89,38],{}," 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 ",[27,92,38],{}," in DevTools and blocks it silently on slow connections. GTM fires tags asynchronously after the page begins loading. This means nearly every ",[27,95,38],{}," call inside a GTM Custom HTML tag executes post-load, guaranteeing the destructive path.",[59,98,100],{"id":99},"example","Before \u002F after",[102,103,104,154],"compare",{},[105,106,108],"compare-col",{"label":107},"Before — flagged",[109,110,115],"pre",{"className":111,"code":112,"language":113,"meta":114,"style":114},"language-js shiki shiki-themes github-light github-dark","\u002F\u002F Injects a vendor script via document.write — flagged\ndocument.write('\u003Cscript src=\"https:\u002F\u002Fvendor.example\u002Ftag.js\">\u003C\\\u002Fscript>')\n","js","",[27,116,117,126],{"__ignoreMap":114},[118,119,122],"span",{"class":120,"line":121},"line",1,[118,123,125],{"class":124},"sJ8bj","\u002F\u002F Injects a vendor script via document.write — flagged\n",[118,127,129,133,137,140,144,148,151],{"class":120,"line":128},2,[118,130,132],{"class":131},"sVt8B","document.",[118,134,136],{"class":135},"sScJk","write",[118,138,139],{"class":131},"(",[118,141,143],{"class":142},"sZZnC","'\u003Cscript src=\"https:\u002F\u002Fvendor.example\u002Ftag.js\">\u003C",[118,145,147],{"class":146},"sj4cs","\\\u002F",[118,149,150],{"class":142},"script>'",[118,152,153],{"class":131},")\n",[105,155,157],{"label":156},"After — fixed",[109,158,160],{"className":111,"code":159,"language":113,"meta":114,"style":114},"\u002F\u002F Async DOM injection — no parser blocking\nconst s = document.createElement('script')\ns.src = 'https:\u002F\u002Fvendor.example\u002Ftag.js'\ndocument.head.appendChild(s)\n",[27,161,162,167,192,204],{"__ignoreMap":114},[118,163,164],{"class":120,"line":121},[118,165,166],{"class":124},"\u002F\u002F Async DOM injection — no parser blocking\n",[118,168,169,173,176,179,182,185,187,190],{"class":120,"line":128},[118,170,172],{"class":171},"szBVR","const",[118,174,175],{"class":146}," s",[118,177,178],{"class":171}," =",[118,180,181],{"class":131}," document.",[118,183,184],{"class":135},"createElement",[118,186,139],{"class":131},[118,188,189],{"class":142},"'script'",[118,191,153],{"class":131},[118,193,195,198,201],{"class":120,"line":194},3,[118,196,197],{"class":131},"s.src ",[118,199,200],{"class":171},"=",[118,202,203],{"class":142}," 'https:\u002F\u002Fvendor.example\u002Ftag.js'\n",[118,205,207,210,213],{"class":120,"line":206},4,[118,208,209],{"class":131},"document.head.",[118,211,212],{"class":135},"appendChild",[118,214,215],{"class":131},"(s)\n",[59,217,219],{"id":218},"remediation","Remediation",[221,222,223,233,255,266,269],"ol",{},[20,224,225,226,228,229,232],{},"Open each flagged tag and identify what ",[27,227,38],{}," is injecting — typically a ",[27,230,231],{},"\u003Cscript>"," tag or an HTML block.",[20,234,235,236,239,240,243,244,247,248,243,251,254],{},"For script injection: create the element with ",[27,237,238],{},"document.createElement('script')",", set its ",[27,241,242],{},"src"," or ",[27,245,246],{},"textContent",", and append it with ",[27,249,250],{},"document.head.appendChild()",[27,252,253],{},"document.body.appendChild()",".",[20,256,257,258,261,262,265],{},"For HTML injection: assign to ",[27,259,260],{},"innerHTML"," on a target element, or use ",[27,263,264],{},"insertAdjacentHTML()"," for positional control.",[20,267,268],{},"Test in GTM Preview mode to confirm the replaced logic loads correctly.",[20,270,271],{},"Publish once verified.",[59,273,275],{"id":274},"metadata","Metadata",[277,278,281,282,287,281,291,295,281,298,302,281,305,308,281,311,315,281,318,322,281,328,331],"dl",{"className":279},[280],"spec-list","\n  ",[283,284,286],"dt",{"id":285},"severity","Severity",[288,289,290],"dd",{},"high",[283,292,294],{"id":293},"tier","Tier",[288,296,297],{},"optimization",[283,299,301],{"id":300},"category","Category",[288,303,304],{},"performance",[283,306,307],{"id":50},"Layer",[288,309,310],{},"container",[283,312,314],{"id":313},"version","Version",[288,316,317],{},"1",[283,319,321],{"id":320},"effort","Effort",[288,323,324,327],{},[27,325,326],{},"2hr"," (about 2 hours)",[283,329,330],{"id":56},"Reversibility",[288,332,333],{},"non-destructive",[59,335,337],{"id":336},"see-also","See also",[17,339,340,348,355],{},[20,341,342,347],{},[343,344,346],"a",{"href":345},"\u002Fcategories\u002Fperformance\u002F","Performance rules"," — other rules in this category",[20,349,350,354],{},[343,351,353],{"href":352},"\u002Fhealth-score\u002F","Health score"," — how this rule contributes to the score",[20,356,357,361],{},[343,358,360],{"href":359},"\u002Frules\u002F","Rules catalog"," — browse all rules",[363,364,365],"style",{},"html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":114,"searchDepth":128,"depth":128,"links":367},[368,369,370,371,372,373],{"id":61,"depth":128,"text":62},{"id":84,"depth":128,"text":85},{"id":99,"depth":128,"text":100},{"id":218,"depth":128,"text":219},{"id":274,"depth":128,"text":275},{"id":336,"depth":128,"text":337},[375,378,380],{"label":376,"href":377},"API Reference","\u002F",{"label":379,"href":359},"Rules",{"label":29},{"cards":382,"onpage":394},[383],{"title":384,"tabs":385},"Evaluate",[386,390],{"label":387,"language":388,"code":389},"cURL","bash","curl https:\u002F\u002Fdeveloper.tagmanifest.com\u002Fv1\u002Fevaluate-rule \\\n  -H \"Authorization: Bearer $TM_API_KEY\" \\\n  -H \"Content-Type: application\u002Fjson\" \\\n  -d '{\"rule_id\":\"document-write\",\"url\":\"https:\u002F\u002Fexample.com\"}'\n",{"label":391,"language":392,"code":393},"MCP","text","User: Run document-write on https:\u002F\u002Fexample.com\n\n# Tool call\n{\n  \"tool\": \"evaluate_rule\",\n  \"arguments\": {\n    \"rule_id\": \"document-write\",\n    \"url\": \"https:\u002F\u002Fexample.com\"\n  }\n}\n",[395,396,397,398,399],{"id":61,"label":62},{"id":84,"label":85},{"id":99,"label":100},{"id":218,"label":219},{"id":274,"label":275},null,"Detects Custom HTML tags using document.write(), which blocks page rendering and can cause blank pages when called after initial load.","md",{},true,"\u002Frules\u002Fdocument-write","TechArticle",{"title":5,"description":401},"rules\u002Fdocument-write",false,"szPtJtZBsjZgLPqe9pft3GXRdwNXhXuY0gdyfmh7mow",1796069484160]