JavaScript Document open() Method

Beginner
⏱️ 14 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

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

Introduction

Before modern DOM APIs, many scripts built entire pages with a document stream:

  1. document.open() — start a new input stream (replaces the document).
  2. document.write(...) — push HTML strings into that stream.
  3. 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.

Related tutorials: close(), clear(), prepend(), parseHTML().

Understanding document.open()

An instance method on the Document interface for dynamic markup insertion (MDN / HTML).

  • Usual callopen() 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-opendocument.write() after load can call open() for you (MDN Notes).

📝 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();

Safer practice pattern (iframe)

JavaScript
const frame = document.getElementById("preview");
const doc = frame.contentDocument;

doc.open();
doc.write("<!DOCTYPE html><title>Preview</title><p>Hello from stream</p>");
doc.close();

Other document.open() forms (MDN)

Three-argument form — Window.open alias

MDN: a lesser-known three-argument version is an alias of Window.open(). Example opens GitHub in a new window with opener set to null:

JavaScript
document.open("https://www.github.com", "", "noopener=true");

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.

⚡ Quick Reference

GoalCode
Start streamdocument.open()
Full patternopen(); write(...); close();
Safe demo targetiframe.contentDocument.open()
Return valueDocument instance
Open a windowwindow.open(url, name, features) (prefer over 3-arg document.open)
MDN statusBaseline Widely available

🔍 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

📋 Stream APIs vs modern DOM updates

GoalLegacy streamModern approach
Replace iframe contentdoc.open/write/closeiframe.srcdoc = "..."
Build UI from datadocument.writeCreate elements / templates
Parse HTML stringwrite into streamDocument.parseHTML() / DOMParser
Open a new tab3-arg document.openwindow.open

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();
Try It Yourself

How It Works

open() clears and starts the stream; write() adds markup; close() finishes it (MDN).

Example 2 — Return value is a Document

MDN: open() returns a Document instance.

JavaScript
const doc = document.getElementById("preview").contentDocument;
const opened = doc.open();

console.log(opened === doc); // true in normal cases
opened.write("<p>Returned document</p>");
opened.close();
Try It Yourself

How It Works

You can chain from the returned document, but most code just reuses doc.

📈 Side Effects & Alternatives

See what open clears, and prefer modern APIs when possible.

Example 3 — Nodes are cleared by open()

MDN: all existing nodes are removed from the document.

JavaScript
const doc = document.getElementById("preview").contentDocument;

doc.open();
doc.write("<p id='old'>Old content</p>");
doc.close();

const before = !!doc.getElementById("old");
doc.open(); // clears previous nodes
const afterOpen = !!doc.getElementById("old");
doc.write("<p>New content</p>");
doc.close();

console.log({ before, afterOpen });
Try It Yourself

How It Works

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.
Try It Yourself

How It Works

Implicit open surprises beginners because it also clears the document. Make open() explicit.

Example 5 — Modern alternative: srcdoc

For iframe previews, skip the stream APIs.

JavaScript
const frame = document.getElementById("preview");
frame.srcdoc = "<!DOCTYPE html><title>Preview</title><p>Hello without open/write/close</p>";
Try It Yourself

How It Works

Learn open() for legacy literacy; prefer srcdoc, createElement, or parseHTML() for new work.

🚀 Common Use Cases

  • Legacy stream scripts — rebuild a document with write/close (MDN).
  • Iframe previews — open/write/close into contentDocument.
  • Understanding auto-open — why late document.write wipes a page (MDN Notes).
  • Not for SPA UI — use DOM methods instead of rewriting the document.
  • Not for new windows — call window.open rather than 3-arg document.open.
  • Same-origin constrained — cannot change the document origin (MDN).

🧠 How document.open() Starts a Stream

1

Call document.open()

Opens the document for writing (MDN).

Open
2

Document is cleared

Nodes and listeners are removed (MDN side effects).

Reset
3

write() fills the stream

Push HTML fragments into the open document.

Write
4

close() finishes parsing

See the document.close() tutorial.

📝 Notes

  • MDN: opens a document for writing; returns a Document instance.
  • Not Deprecated, Experimental, or Non-standard on MDN — Baseline Widely available.
  • MDN: removes listeners and existing nodes as side effects.
  • MDN: automatic open() can occur when write() runs after load.
  • MDN: two-argument form is obsolete; three-argument form aliases Window.open().
  • Related: close(), clear(), parseHTML().

Browser Support

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.

Baseline Widely available
Google Chrome Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Microsoft Edge Supported
Yes
Opera Supported
Yes
Internet Explorer Supported (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.

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.

Continue with close(), parseHTML(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Call open()write()close() as one unit
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about document.open()

Start a write stream — and know the cost.

5
Core concepts
🔄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.

Next: prepend()

Learn how Document.prepend() inserts nodes or strings before the first child of a Document.

prepend() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful