We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

document.write Redirect 1

« DOM Reference


Writes a string of text to a document stream opened by document.open().


  • markup is a string containing the text to be written to the document.



<title>write example</title>

<script type="text/javascript">

function newContent()
alert("load new content");
document.write("<h1>Out with the old - in with the new!</h1>");


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


Writing to a document that has already loaded without calling document.open() will automatically perform a document.open call. 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 directly in the HTML code, then it will not call document.open(). For example:

  <script type="text/javascript"> 
    document.write("<h1>Main title</h1>") 
Note: document.write (like document.writeln) does not work in XHTML documents (you'll get a "Operation is not supported" (NS_ERROR_DOM_NOT_SUPPORTED_ERR) error on the error console). This is the case if opening a local file with a .xhtml file extension or for any document served with an application/xhtml+xml MIME type. More information is available in the W3C XHTML FAQ.


See also

Document Tags and Contributors

Last updated by: Sheppy,