The Document.write() method writes a string of text to a document stream opened by

Note: Because document.write() writes to the document stream, calling document.write() on a closed (loaded) document automatically calls, which will clear the document.




A string containing the text to be written to the document.



  <title>Write example</title>

    function newContent() {;
      document.write("<h1>Out with the old, in with the new!</h1>");

<body onload="newContent();">
  <p>Some original document content.</p>



The text you write is parsed into the document's structure model. In the example above, the h1 element becomes a node in the document.

Writing to a document that has already loaded without calling will automatically call After writing, call document.close() to tell the browser to finish loading the page.

If the document.write() call is embedded within an inline HTML <script> tag, then it will not call For example:

  document.write("<h1>Main title</h1>") 
Note: document.write() and document.writeln do not work in XHTML documents (you'll get an "Operation is not supported" [NS_ERROR_DOM_NOT_SUPPORTED_ERR] error in the error console). This happens when opening a local file with the .xhtml file extension or for any document served with an application/xhtml+xml MIME type. More information is available in the W3C XHTML FAQ.
Note: Using document.write() in deferred or asynchronous scripts will be ignored and you'll get a message like "A call to document.write() from an asynchronously-loaded external script was ignored" in the error console.
Note: In Edge only, calling document.write() more than once in an <iframe> causes the error "SCRIPT70: Permission denied".
Note: Starting with version 55, Chrome will not execute <script> elements injected via document.write() when specific conditions are met. For more information, refer to Intervening against document.write().


Specification Status Comment
HTML Living Standard
The definition of 'document.write(...)' in that specification.
Living Standard
Document Object Model (DOM) Level 2 HTML Specification
The definition of 'document.write(...)' in that specification.

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
writeChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0


Full support  
Full support

See also