document.write() is a Deprecated instance method that writes one or more HTML strings (or TrustedHTML values) into a document stream opened by document.open() (see MDN Document: write()). Learn the classic open() → write() → close() pattern, post-load wipe risks, XSS / Trusted Types notes, safer alternatives, and five iframe-based try-it labs.
01
Kind
Instance method
02
Args
1+ markup strings
03
Returns
undefined
04
Pairs with
open / close
05
Risk
XSS sink
06
Status
Deprecated
Fundamentals
Introduction
Early web scripts often built pages by writing HTML into a document stream:
");
doc.close();
// Later, after that document is loaded:
document.getElementById("wipe").addEventListener("click", () => {
// Calling write on a loaded document can open() and replace it (MDN).
doc.write("
Original content disappears; only the late write remains
How It Works
This is the classic beginner bug when someone calls document.write() from a button on the main page.
Example 5 — Prefer createElement + append
Same visual result without a deprecated stream API.
JavaScript
const host = document.getElementById("host");
host.replaceChildren();
const h1 = document.createElement("h1");
h1.textContent = "Out with the old";
const p = document.createElement("p");
p.textContent = "in with the new!";
host.append(h1, p);
Document.write() is Deprecated on MDN and strongly discouraged, but remains widely implemented for legacy compatibility. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Deprecated · Legacy
Document.write()
Write markup into a document stream opened by document.open(). Prefer modern DOM APIs for new code.
LegacyDeprecated
Google ChromeSupported (legacy)
Yes*
Mozilla FirefoxSupported (legacy)
Yes*
Apple SafariSupported (legacy)
Yes*
Microsoft EdgeSupported (legacy)
Yes*
OperaSupported (legacy)
Yes*
Internet ExplorerSupported (legacy)
Yes*
write()Avoid in new apps
Bottom line: Learn open/write/close for literacy. Never inject untrusted HTML. Rebuild UI with createElement, append, and replaceChildren instead.
Wrap Up
Conclusion
document.write() is a Deprecated stream writer that pairs with open() and close(). Learn it so legacy code makes sense, respect the after-load wipe and XSS risks, then build with modern DOM APIs.
Migrate to createElement / append / replaceChildren
Use Trusted Types if you must write HTML under enforcement (MDN)
❌ Don’t
Use write() in new products (MDN)
Call it on the live page after load
Pass untrusted user HTML strings
Assume parser quirks will match your source
Confuse document.write with console.log debugging
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about write()
Deprecated document-stream HTML writing.
5
Core concepts
📝01
Returns
undefined
MDN
📄02
Stream
open/write/close
pattern
🗑03
After load
can wipe
danger
🛡04
Security
XSS sink
MDN
⚠️05
Status
Deprecated
MDN
❓ Frequently Asked Questions
MDN: Document.write() writes text in one or more TrustedHTML or string parameters to a document stream opened by document.open().
Yes. MDN marks Document.write() as Deprecated and strongly discourages using it. Prefer modern DOM APIs such as createElement, textContent, append, or carefully sanitized HTML insertion.
None (undefined) (MDN).
MDN Notes (via Document.open): an automatic document.open() call can happen, which clears the existing document. That is why a late write() can wipe the whole page.
Yes. MDN: write() parses input as HTML and is an injection sink. Never pass untrusted user content. Prefer TrustedHTML when Trusted Types are enforced.
Do not call document.write() on the live tutorial page. Use an iframe’s contentDocument with open(), write(), and close(), as in the try-it labs.
Did you know?
MDN notes that a late document.write() after the page has loaded can trigger an automatic document.open(). That is why a single debugging write() can erase an entire page and look like a mysterious bug.