document.open() is an instance method that opens a document for writing (see MDN Document: open()). Learn the classic open() → write() → close() stream, destructive side effects, the three-argument Window.open alias, and safer modern alternatives.
01
Kind
Instance method
02
Args
None (usual)
03
Returns
Document
04
Pairs with
write / close
05
Side effect
Clears page
06
Status
Baseline
Fundamentals
Introduction
Before modern DOM APIs, many scripts built entire pages with a document stream:
document.open() — start a new input stream (replaces the document).
document.write(...) — push HTML strings into that stream.
document.close() — finish the stream so the browser can complete parsing.
MDN: Document.open() opens a document for writing. That sounds simple — but it has strong side effects.
⚠️
Side effects (MDN)
Calling open() removes all event listeners currently registered on the document, nodes inside it, or the document’s window — and removes all existing nodes from the document.
💡
Practice on an iframe
Never run open/write/close on the current tutorial page. Use iframe.contentDocument so only the preview is replaced.
An instance method on the Document interface for dynamic markup insertion (MDN / HTML).
Usual call — open() with no parameters (MDN).
Return value — a Document object instance (MDN).
Clears content — existing nodes are removed (MDN).
Clears listeners — on document, descendants, and window (MDN).
Same-origin — will not work if it would change the document origin (MDN).
Auto-open — document.write() after load can call open() for you (MDN Notes).
Foundation
📝 Syntax
Usual form of Document.open (MDN):
JavaScript
open()
Parameters
None for the standard writing form (MDN).
Return value
A Document object instance (MDN).
MDN classic example
JavaScript
document.open();
document.write("<p>Hello world!</p>");
document.write("<p>I am a fish</p>");
document.write("<p>The number is 42</p>");
document.close();
Prefer calling window.open(...) explicitly so readers are not confused with the document-stream API.
Two-argument form — obsolete
MDN: browsers used to support document.open(type, replace) (MIME type + optional "replace" history behavior). That form is obsolete. It does not throw; it forwards to no-arg document.open(). History replacement now always happens.
window.open(url, name, features) (prefer over 3-arg document.open)
MDN status
Baseline Widely available
Snapshot
🔍 At a Glance
Four facts about document.open().
Role
start stream
for write
Returns
Document
MDN
Clears
nodes+events
MDN
Finish with
close()
after write
Compare
📋 Stream APIs vs modern DOM updates
Goal
Legacy stream
Modern approach
Replace iframe content
doc.open/write/close
iframe.srcdoc = "..."
Build UI from data
document.write
Create elements / templates
Parse HTML string
write into stream
Document.parseHTML() / DOMParser
Open a new tab
3-arg document.open
window.open
Hands-On
Examples Gallery
Examples follow MDN Document: open(). Try-it labs use an iframe so the editor page is not replaced.
📚 Getting Started
Open a stream, write markup, then close.
Example 1 — MDN: open, write, close
Replace iframe content with several HTML fragments.
JavaScript
const doc = document.getElementById("preview").contentDocument;
doc.open();
doc.write("<p>Hello world!</p>");
doc.write("<p>I am a fish</p>");
doc.write("<p>The number is 42</p>");
doc.close();
Treat open() as a full document reset for that browsing context’s document content and listeners (MDN).
Example 4 — write() can auto-open after load
MDN Notes: writing after the page has loaded triggers an automatic open().
JavaScript
// Conceptual note (do this on an iframe in demos):
const doc = document.getElementById("preview").contentDocument;
// Explicit is clearer for beginners:
doc.open();
doc.write("<p>Explicit open</p>");
doc.close();
// After a document is fully loaded, a lone write() may open implicitly (MDN).
// Prefer always calling open() yourself so the stream is intentional.
Document.open() is Baseline Widely available on MDN. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.open()
Opens a document for writing and starts the classic open/write/close stream — widely supported.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
open()Wide
Bottom line: Supported across modern browsers. Prefer DOM APIs or iframe.srcdoc for new UI code; use open/write/close when maintaining stream-based scripts.
Wrap Up
Conclusion
document.open() starts a document input stream for document.write(), clearing existing nodes and listeners in the process. Always finish with document.close(), practice on an iframe, and prefer modern DOM / srcdoc for new apps.
Practice on iframe.contentDocument, not the live page
Prefer srcdoc or DOM APIs for new iframe previews
Use window.open for new windows/tabs
Remember side effects clear nodes and listeners (MDN)
❌ Don’t
Call open() on the current page by accident
Rely on the obsolete two-argument form (MDN)
Confuse stream open() with window.open()
Build new SPAs primarily with document.write
Skip close() after writing
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about document.open()
Start a write stream — and know the cost.
5
Core concepts
📝01
Returns
Document
MDN
🔄02
Clears
nodes+events
MDN
🎯03
Next
write+close
stream
⚡04
3-arg
Window.open
alias
🛡05
Status
Baseline
MDN
❓ Frequently Asked Questions
MDN: Document.open() opens a document for writing. Side effects include removing all event listeners on the document, its nodes, and its window, and removing all existing nodes from the document.
No. MDN does not mark Document.open() as Deprecated, Experimental, or Non-standard. It is Baseline Widely available. Prefer modern DOM APIs for new apps, but the method itself is a standard part of dynamic markup insertion.
A Document object instance (MDN). Typically the same document you called open() on.
MDN Notes: an automatic document.open() call happens when document.write() is called after the page has loaded.
Yes. MDN: the lesser-known three-argument document.open(url, name, features) is an alias of Window.open(). Example: document.open("https://www.github.com", "", "noopener=true").
MDN: document.open(type, replace) is obsolete. It no longer throws; it forwards to document.open() with no arguments. History replacement now always happens.
Did you know?
MDN notes that a late document.write() after the page has loaded can automatically call document.open() — which is why a stray write() can wipe an entire page and look like a mysterious bug.