JavaScript Document close() Method

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

What You’ll Learn

document.close() is an instance method that finishes writing to a document opened with document.open() (see MDN Document: close()). Learn the classic open()write()close() stream, what close() returns, how it differs from window.close(), and safer modern alternatives — with five examples and try-it labs.

01

Kind

Instance method

02

Args

None

03

Returns

undefined

04

Pairs with

open / write

05

Not

window.close

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.close() finishes writing to a document opened with Document.open(). Without close(), some browsers may delay treating the streamed document as fully loaded.

⚠️
Do not run this on your live tutorial page

open() / write() / close() on the current document replaces that page. Prefer an iframe’s contentDocument for practice, as in the try-it labs below.

Related tutorials: clear(), append(), parseHTML().

Understanding document.close()

An instance method on the Document object. Part of the HTML dynamic markup insertion APIs (MDN).

  • Parameters — none (MDN).
  • Return valueundefined (MDN).
  • Purpose — close the input stream started by document.open().
  • Typical orderopen()write()close().
  • Not a window closer — that is window.close().
  • Status — Baseline Widely available; not Deprecated on MDN.

📝 Syntax

General form of Document.close (MDN):

JavaScript
close()

Parameters

None (MDN).

Return value

undefined (MDN).

Classic stream pattern (MDN)

JavaScript
// Open a document to write to it
document.open();

// Write the content of the document
document.write("<p>The one and only content.</p>");

// Close the document
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();

⚡ Quick Reference

GoalCode
Finish streamdocument.close()
Full patternopen(); write(...); close();
Safe demo targetiframe.contentDocument.close()
Return valueundefined
Feature-detecttypeof document.close === "function"
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.close().

Role
end stream

after write

Returns
undefined

always

Needs
open()

first

Not
window.close

different

📋 Stream APIs vs modern DOM updates

GoalLegacy streamModern approach
Replace iframe contentdoc.open/write/closeiframe.srcdoc = "..."
Empty a containerNot what close() doesel.replaceChildren()
Build UI from datadocument.writeCreate elements / templates
Parse HTML stringwrite into streamDocument.parseHTML() / DOMParser

Examples Gallery

Examples follow MDN Document: close(). Try-it labs use an iframe so the editor page is not replaced.

📚 Getting Started

Finish a document stream with close().

Example 1 — MDN pattern: open, write, close

Build iframe content with the classic stream APIs.

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

doc.open();
doc.write("<p>The one and only content.</p>");
doc.close();
Try It Yourself

How It Works

open() starts the stream, write() adds markup, close() finishes writing (MDN).

Example 2 — Return value is undefined

Capture what close() returns after a write session.

JavaScript
const doc = iframe.contentDocument;

doc.open();
doc.write("<p>Hi</p>");
const result = doc.close();

console.log(result);                 // undefined
console.log(result === undefined);   // true
Try It Yourself

How It Works

MDN lists the return value as undefined. Do not expect a success boolean.

📈 Practical Patterns

Multiple writes, readyState, and modern replacements.

Example 3 — Multiple write() calls, then one close()

You can stream several chunks before finishing.

JavaScript
const doc = iframe.contentDocument;

doc.open();
doc.write("<!DOCTYPE html><html><body>");
doc.write("<h1>Title</h1>");
doc.write("<p>Paragraph</p>");
doc.write("</body></html>");
doc.close();
Try It Yourself

How It Works

Call close() once after all writes. That ends the input stream for the browser.

Example 4 — Check readyState after close()

After closing, the streamed document should reach a complete state.

JavaScript
const doc = iframe.contentDocument;

doc.open();
doc.write("<p>Done</p>");
doc.close();

console.log(doc.readyState); // often "complete" after close
Try It Yourself

How It Works

close() signals the end of streamed markup so loading can complete.

Example 5 — Modern alternative: srcdoc (no stream)

For iframe previews, prefer srcdoc over open/write/close.

JavaScript
const iframe = document.getElementById("preview");

iframe.srcdoc = "<!DOCTYPE html><p>Hello without document.write</p>";

// Equivalent intent without open / write / close
// Prefer DOM APIs on the main page too:
document.getElementById("panel").replaceChildren(
  Object.assign(document.createElement("p"), { textContent: "Hello" })
);
Try It Yourself

How It Works

srcdoc sets iframe HTML in one step. On the main page, create or replace nodes with DOM methods instead of streaming.

🚀 Common Use Cases

  • Legacy page builders — finish a document after document.write() (MDN).
  • Iframe previews — stream HTML into contentDocument then close().
  • Maintaining old code — understand why scripts call close() after writes.
  • Teaching streams — show open/write/close as one unit.
  • Not for closing tabs — use window.close() for that (different API).
  • Prefer modern DOM — for new apps, use elements, templates, or srcdoc.

🧠 How close() Works

1

Call document.open()

Starts (or replaces) the document input stream for writing.

Open
2

Write markup

document.write() / writeln() push HTML strings into the stream.

Write
3

Call document.close()

MDN: finishes writing to the document opened with open().

Close
4

Stream complete

Returns undefined. The browser can finish parsing the streamed document.

📝 Notes

  • MDN: finishes writing to a document opened with Document.open().
  • Not Deprecated, Experimental, or Non-standard on MDN — Baseline Widely available.
  • Do not confuse with window.close() (closes a browsing context).
  • Do not confuse with document.clear() (deprecated no-op).
  • Calling open/write/close on the current page replaces that page — use an iframe for demos.
  • Related: open(), clear(), append().

Browser Support

Document.close() is Baseline Widely available on MDN. Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Document.close()

Finishes a document stream opened with document.open() — 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
close() 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.close() finishes a document input stream started with document.open() after you have written markup with document.write(). It returns undefined, is Baseline Widely available, and is not the same as window.close().

Continue with createAttribute(), append(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Call close() after open() + write() (MDN)
  • Practice on iframe.contentDocument, not the live page
  • Prefer srcdoc or DOM APIs for new iframe previews
  • Remember the return value is undefined
  • Keep open / write / close as one intentional unit

❌ Don’t

  • Confuse document.close() with window.close()
  • Call open/write/close on the current page by accident
  • Expect close() to empty an element (use replaceChildren)
  • Build new SPAs primarily with document.write
  • Skip close() when using the stream pattern

Key Takeaways

Knowledge Unlocked

Five things to remember about close()

Ends the open/write document stream.

5
Core concepts
🔗02

Order

open → write

then close
📄03

Returns

undefined

always
⚠️04

Not

window.close

different
🛡05

Prefer

DOM / srcdoc

new code

❓ Frequently Asked Questions

MDN: Document.close() finishes writing to a document that was opened with Document.open(). It closes the document's input stream so the browser can finish parsing content written with document.write().
No. MDN does not mark Document.close() 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.
undefined (MDN). It takes no parameters.
After document.open() and any document.write() / document.writeln() calls that build the new document content. close() tells the browser the stream is finished.
No. document.close() finishes a document input stream opened with document.open(). window.close() tries to close a browser window or tab. They are different APIs.
Calling open/write/close on the current page replaces that page's document. Demos use an iframe's contentDocument so the tutorial UI stays intact while you practice the stream APIs safely.
Did you know?

MDN’s official example is only three calls — document.open(), one document.write(), then document.close() — because close() exists specifically to finish that writing session, not to clear the DOM or close a browser tab.

Next: createAttribute()

Learn how to create Attr nodes and attach them with setAttributeNode().

createAttribute() →

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