Screpy - AI SEO Audit Tool

document.write(): Why It Is Risky and How to Replace It

document.write is deprecated, parser-dependent, and potentially unsafe. Replace it with explicit DOM APIs or modern script loading patterns.

Reviewed by Screpy Editorial Team

Why document.write is flagged

document.write() writes text into the document stream while HTML is being parsed. Its behavior depends on timing and browser parsing state. It can block rendering, produce unpredictable results in deferred or asynchronous scripts, and overwrite the current page when called after loading.

MDN marks the method as deprecated and warns that it is an injection sink when untrusted input reaches it. New code should not use it.

Find the source

  1. Search first-party source, templates, tag-manager snippets, and generated HTML for document.write.
  2. Use browser developer tools to identify the script and call stack responsible for the call.
  3. Check advertising, analytics, consent, and legacy widget providers.
  4. Confirm whether the call runs during initial parsing or after the document has loaded.

Replace markup insertion

Create elements explicitly and add text through textContent when the value should not be interpreted as HTML.

const message = document.createElement('p');
message.textContent = statusText;
document.querySelector('[data-status]')?.append(message);

When trusted application markup must be rendered, use the framework or templating system already responsible for that part of the page. Avoid moving untrusted strings into innerHTML as a one-line replacement because that preserves the injection risk.

Replace script injection

const script = document.createElement('script');
script.src = 'https://example.com/widget.js';
script.async = true;
document.head.append(script);

Prefer the official modern integration offered by the provider. Preserve consent, integrity, nonce, and Content Security Policy requirements used by the site.

Third-party code you cannot edit

Ask the provider for an asynchronous or module-based snippet. Load the integration only on pages that need it. If the vendor offers no safe replacement, evaluate removal or an isolated implementation rather than suppressing the audit.

Security considerations

Never pass user-controlled input to document.write, innerHTML, insertAdjacentHTML, or a similar HTML parser without an appropriate sanitization and Trusted Types strategy. Use textContent for plain text and project-approved rendering utilities for markup.

Verify the replacement

Test with JavaScript enabled under slow-network conditions. Confirm that the same content appears, scripts execute once, analytics and consent still work, and no console or Content Security Policy errors are introduced. Compare rendering and performance before and after.

See the MDN Document.write reference for current browser and security guidance.

Related tasks

Solve the errors logged to the console

If there are error warnings about your website within the console offered by the browser, you need to follow a few basic steps to resolve them.

Tuesday, December 22, 2020

Title Tags Not Found

Are you encountering the title meta tag not found the issue? You can solve it right away! Here are the causes and importance of the problem in detail.

Sunday, December 20, 2020

Robots.txt Not Found

If you are encountering the Robots.txt not found error, you can create your file immediately by paying attention to the steps below. Discover the details now!

Sunday, December 20, 2020

Not Crawlable Pages

Have the same problem with crawlability issues? Let’s see what you can do for your website to be indexed. Here are the ways and solutions!

Sunday, December 20, 2020

Meta Description Not Found

Are you getting a meta description not found a warning about your web page? Let's figure it out! Why is meta description important, how to pass the audit? Examine!

Sunday, December 20, 2020