JavaScript Document close() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.close() finishes writing to a document that was opened with document.open(). After write() / writeln(), call close() so the browser can finish parsing the stream. It returns undefined (MDN).

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

Syntax

Call document.close. Start with this line.

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

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

Library declarations
JavaScript
document.close()

Parameters

ParameterDescription
Prior open()Required. Starts the document input stream

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Finish a Document Stream

MDN pattern on an iframe so the host page is not replaced.

JavaScript
const iframe = document.createElement("iframe");
document.body.appendChild(iframe);

const doc = iframe.contentDocument;
doc.open();
doc.write("<p>The one and only content.</p>");
doc.close();

console.log(doc.body.textContent.trim()); // "The one and only content."
console.log(doc.body.childElementCount);  // 1
Try It Yourself

How It Works

  1. Use an iframe. Stream APIs replace the target document safely.
  2. Open. open() starts the input stream.
  3. Write. write() adds markup.
  4. Close. close() finishes the stream so content can render.

Example 2 — Return Value Is undefined

Confirm the method exists and returns nothing useful to chain on.

JavaScript
const iframe = document.createElement("iframe");
document.body.appendChild(iframe);
const doc = iframe.contentDocument;

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

console.log(typeof doc.close); // "function"
console.log(result);           // undefined
console.log(typeof result);    // "undefined"
Try It Yourself

How It Works

  1. Complete the stream. open → write → close on the iframe doc.
  2. Capture the return. Store doc.close() in a variable.
  3. Check types. The method is a function; the result is undefined.
  4. No chaining. Do not call another method on the return value.

Example 3 — document.close() vs window.close()

Same word, different jobs — stream finish vs closing a window.

JavaScript
console.log(typeof document.close); // "function"
console.log(typeof window.close);   // "function"
console.log(document.close === window.close); // false

// document.close() → finish open/write stream (Deprecated)
// window.close()   → attempt to close a window/tab
Try It Yourself

How It Works

  1. Both exist. Each API exposes a close function.
  2. Different objects. One lives on Document, one on Window.
  3. Not equal. They are separate functions with separate jobs.
  4. Pick carefully. Stream finish ≠ closing a tab. See window.close().

Browser Support

Document.close() is Deprecated on MDN as part of dynamic markup insertion, but remains widely implemented for legacy compatibility.

✓ Deprecated · Legacy stream API

Document.close()

Still present in modern browsers for open/write streams. Prefer DOM APIs for new work.

Legacy Deprecated API
Google Chrome Legacy stream support
Legacy
Microsoft Edge Legacy stream support
Legacy
Opera Legacy stream support
Legacy
Mozilla Firefox Legacy stream support
Legacy
Apple Safari Legacy stream support
Legacy
Internet Explorer Legacy
Legacy
document.close() Literacy only

Bottom line: Educational / legacy reference. See the MDN Document.close() reference.

Frequently Asked Questions

MDN: Document.close() finishes writing to a document opened with Document.open(). It closes the document input stream after document.write() / writeln() so the browser can finish parsing.
Yes. MDN marks Document.close() as Deprecated with the dynamic markup insertion API (including document.write()). Prefer modern DOM APIs for new code.
undefined (MDN). It takes no parameters.
After document.open() and any write/writeln calls that build the new content. close() signals that the stream is finished.
No. document.close() finishes a document input stream. window.close() tries to close a browser window or tab.
Calling open/write/close on the current page replaces that page. Demos use an iframe contentDocument so the tutorial UI stays intact.

Next: createAttribute()

Learn how to create an Attr node with document.createAttribute().

createAttribute() 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.

11 people found this page helpful