The Document.write()
method writes a string of text to a document stream opened by document.open()
.
Note: as
document.write
writes to the document stream, calling document.write
on a closed (loaded) document automatically calls document.open
, which will clear the document.Syntax
document.write(markup);
Parameters
- markup
- A string containing the text to be written to the document.
Example
<html> <head> <title>write example</title> <script> function newContent() { document.open(); document.write("<h1>Out with the old - in with the new!</h1>"); document.close(); } </script> </head> <body onload="newContent();"> <p>Some original document content.</p> </body> </html>
Notes
Writing to a document that has already loaded without calling document.open()
will automatically call document.open
. Once you have finished writing, it is recommended to call document.close()
to tell the browser to finish loading the page. 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.
If the document.write()
call is embedded within an inlined HTML <script>
tag, then it will not call document.open()
. For example:
<script> document.write("<h1>Main title</h1>") </script>
Note:
document.write
and document.writeln
do not work in XHTML documents (you'll get a "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:
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 in 55, Chrome will not execute
<script>
elements injected via document.write()
in case of an HTTP cache miss for users on a 2G connection. See here for a list of the conditions that need to be met for this to be true.Specifications
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. |
Obsolete |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
write | Chrome
Full support
45
| Edge Full support 12 | Firefox Full support Yes | IE ? | Opera Full support Yes | Safari Full support Yes | WebView Android
Full support
45
| Chrome Android
Full support
45
| Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android ? |
Legend
- Full support
- Full support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.