JavaScript Document open() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.open() opens a document for writing with document.write(). It clears the document: existing nodes are removed, and event listeners on the document, its nodes, and its window are dropped (MDN). Always finish with document.close().

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call document.open. Start with this line.

JavaScript
document.open()
CallWhat it does
document.open()Primary signature.
document.open(...)Call with the arguments your case needs.

Built into the browser. Call open(...) on document — no import required.

Library declarations
JavaScript
document.open()

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*A Document instance

JavaScript Examples

Example 1 — MDN: open, write, close

Rewrite an iframe document with three paragraphs.

JavaScript
const doc = document.querySelector("iframe").contentDocument;

doc.open();
doc.write("

Hello world!

"); doc.write("

I am a fish

"); doc.write("

The number is 42

"); doc.close(); console.log(doc.querySelectorAll("p").length); // 3
Try It Yourself

How It Works

  1. Target an iframe document. Keeps the parent page safe.
  2. Call open(). Clears that document for a new write stream.
  3. Write HTML strings. Each write adds markup (MDN sample).
  4. Call close(). Finishes the stream; then count the paragraphs.

Example 2 — open() Clears Existing Nodes

Anything already in the document is gone after open().

JavaScript
const doc = document.querySelector("iframe").contentDocument;

doc.open();
doc.write("

old

"); doc.close(); const before = !!doc.getElementById("old"); doc.open(); // clears again const after = !!doc.getElementById("old"); doc.write("

new

"); doc.close(); console.log(before, after); // true false
Try It Yourself

How It Works

  1. Seed content. Write a paragraph with id="old".
  2. Call open() again. MDN: existing nodes are removed.
  3. Check before writing new HTML. getElementById("old") is gone.
  4. Takeaway. Treat open() as a full document reset, not a soft edit.

Example 3 — Prefer Modern DOM

Same visible result without the deprecated stream API.

JavaScript
const host = document.getElementById("host");
host.replaceChildren();

const p = document.createElement("p");
p.textContent = "Hello world!";
host.append(p);

console.log(host.textContent); // "Hello world!"
Try It Yourself

How It Works

  1. Clear with DOM. replaceChildren() empties a host without opening a stream.
  2. Create a node. createElement("p") builds structured content.
  3. Append. No parser quirks from string HTML injection.
  4. Why prefer this. MDN steers new projects to modern DOM over open/write.

Browser Support

Document.open() is widely implemented but marked Deprecated on MDN. Avoid it in new projects even though engines still ship it. See the MDN open() reference.

✓ Deprecated · Widely implemented

Document.open()

Still present in major browsers for legacy dynamic markup insertion. Prefer modern DOM APIs for new code.

Legacy Deprecated API
Google Chrome All modern versions
Yes
Microsoft Edge All modern versions
Yes
Mozilla Firefox All modern versions
Yes
Apple Safari All modern versions
Yes
Opera All modern versions
Yes
Internet Explorer Supported (legacy)
Yes
open() Avoid in new apps

Bottom line: Supported for literacy and legacy pages. For new work, use createElement, append, and replaceChildren instead of open/write/close.

Frequently Asked Questions

It 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 (MDN).
Yes. MDN marks Document.open() as Deprecated. Avoid it in new projects; the dynamic markup insertion API (including document.write()) has idiosyncratic behavior and may be removed from standards or browsers. Prefer modern DOM APIs.
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. The three-argument form document.open(url, name, features) is an alias of Window.open() (MDN). Example: document.open("https://www.github.com", "", "noopener=true").
document.open(type, replace) is obsolete. It no longer throws; it forwards to document.open() with no arguments. History replacement now always happens (MDN).

Next: prepend()

Learn how to insert nodes (and text) at the beginning of a parent.

prepend() tutorial →

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.

15 people found this page helpful