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
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.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.
An instance method on the Document object. Part of the HTML dynamic markup insertion APIs (MDN).
Parameters — none (MDN).
Return value — undefined (MDN).
Purpose — close the input stream started by document.open().
Typical order — open() → write() → close().
Not a window closer — that is window.close().
Status — Baseline Widely available; not Deprecated on MDN.
Foundation
📝 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();
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" })
);
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.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (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.
Wrap Up
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().
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about close()
Ends the open/write document stream.
5
Core concepts
📝01
Role
finish stream
MDN
🔗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.