HTML DOM document.write() method
Document Object
Definition and Usage
document.write() The method can write text content into the document; it can be HTML code.
If it is executed after the document has finished loading, document.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 and will be appended to the document in the order they appear. |
Note:document.write() The method writes a text string into an document.open() output stream opened by (the document stream).
To a document that has already been loaded and has not called document.open() method, writing data will automatically invoke document.open(). Once data writing is complete, it is recommended to call document.close() to tell the browser that the current page has finished loading. The written data is parsed into the Document Object Model (DOM).
If both the document.write() The call occurs in the HTML
Browser support
Chrome Edge Firefox Safari Opera IE Yes Yes Yes Yes Yes Yes All major browsers support the write() method.
Example
Example 1
Write some text to the output stream:
document
.
write
(
"
Hello World!
"
)
;
Try it »
Example 2
Write some HTML code text to the output stream:
document
.
write
(
"
Hello World!
Have a nice day!
"
)
;
Try it »
Example 3
The difference between write() and writeln():
<
p
Note that the write() method does not add a new line after each statement:
p
<
script
document.write("Hello World!"); document.write("Have a nice day!");
script
<
p
Note that the writeln() method adds a new line after each statement:
p
<
script
document.writeln("Hello World!"); document.writeln("Have a nice day!");
script
Try it »
Example 4
If the page is already loaded, this will overwrite the entire HTML document:
function
myFunction
(
)
{
document
.
write
(
"
Hello World
"
)
;
}
Try it »
Example 4
Using open to open the document output stream will overwrite the entire HTML document:
document
.
open
(
)
;
document
.
write
(
"
Hello World
"
)
;
document
.
close
(
)
;
Try it »
Example 5
You can open a new browser window and write content into it:
const
myWindow
=
window
.
open
(
)
;
myWindow
.
document
.
write
(
"
This is a new window
"
)
;
myWindow
.
document
.
write
(
"
Hello World!
"
)
;
Try it »
Document Object