Write some text directly to the HTML document:
More "Try it Yourself" examples below.
The write() method writes HTML expressions or JavaScript code to a document.
The write() method is mostly used for testing: If it is used after an HTML document is fully loaded, it will delete all existing HTML.
Note: When this method is not used for testing, it is often used to write some text to an output stream opened by the document.open() method. See "More Examples" below.
Tip: The document.writeln() method is similar to write(), only it adds a newline character after each statement.
Method | |||||
---|---|---|---|---|---|
write() | Yes | Yes | Yes | Yes | Yes |
Parameter | Description |
---|---|
exp1,exp2,exp3,... | Optional. What to write to the output stream. Multiple arguments can be listed and they will be appended to the document in order of occurrence |
Return Value: | No return value |
---|
Write HTML elements with text directly to the HTML document:
Write the Date object directly to the HTML document:
Using document.write() after an HTML document is fully loaded, will delete all existing HTML.
In this example we illustrate what happens when we put document.write() inside a function. When the function is invoked, all HTML elements will be overwritten and replaced with the new, specified text:
Open an output stream, add some text, then close the output stream:
Open a new window called "MsgWindow", and write some text into it:
Difference between write() and writeln():
Write some text directly to the HTML document, with a new line after each statement (using <br>):