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()
Call
What 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
Parameter
Description
Prior open()
Required. Starts the document input stream
Return Value
Return Value
Description
*
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
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.