HTML DOM document.write()Method
Document Object
Definition and Usage
document.write()The method can write text content to the document, which can be HTML code.
If it is executed after the document has finished loadingdocument.write(), the entire HTML page will be overwritten.
Syntax
document.write(exp1,exp2,exp3,...)
| Parameters | Description |
|---|---|
| exp1,exp2,exp3,... | Optional. The output stream to write to. Multiple parameters can be listed; they will be appended to the document in the order they appear. |
Note:document.write()The method writes a text string into andocument.open()opened document stream (document stream).
To a document that has already been loaded and has not calleddocument.open()when writing data to the document, it will automatically calldocument.open(). Once data writing is completed, it is recommended to call document.close() to tell the browser that the current page has finished loading. The written data will be parsed into the Document Object Model (DOM).
Ifdocument.write()the call occurs in a <script> tag in HTML, then it will not automatically calldocument.open(), as the following code:
<script> document.write("<h1>Main title</h1>") </script>
Browser Support
| Chrome | Edge | Firefox | Safari | Opera | IE |
| Yes | Yes | Yes | Yes | Yes | Yes |
All major browsers support the write() method
Examples
Example 2
Write some HTML code text to the output stream:
Try it Yourself »
Example 3
The difference between write() and writeln():
Try it Yourself »
Example 4
If the page has already loaded, it will overwrite the entire HTML document:
Try it Yourself »
Example 4
Use open to open the document output stream, which will overwrite the entire HTML document:
Try it Yourself »
Example 5
You can open a new browser window and write content:
Try it Yourself »
Document Object Other Extensions