HTML DOM Document write()
Examples
Write some text directly to the HTML output:
Write some HTML elements directly to the HTML output:
Using document.write() after a document is loaded, deletes all existing HTML:
function myFunction() {
document.write("Hello World!");
}
More examples below.
Description
Thewrite() method writes directly to an open (HTML) document stream.
Warning
Thewrite() method deletes all existing HTML when used on a loaded document.
Thewrite() method cannot be used in XHTML or XML.
Note
Thewrite() method is most often used to write to output streams opened by thetheopen() method.
See Also:
Syntax
Parameters
| Parameter | Description |
| exp1,... | Optional. The output stream. Multiple arguments are appended to the document in order of occurrence. |
Return Value
| NONE |
More Examples
Open an output stream, add some HTML, then close the output stream:
document.write("<h1>Hello World</h1>");
document.close();
Open a new window and write some HTML into it:
myWindow.document.write("<h1>New Window</h1>");
myWindow.document.write("<p>Hello World!</p>");
The Difference Between write() and writeln()
The writeln( ) method is only useful when writing to text documents (type=".txt").
Example
document.write("Have a nice day!");
document.write("<br>");
document.writeln("Hello World!");
document.writeln("Have a nice day!");
Note
It makes no sense to usewriteln() in HTML.
It is only useful when writing to text documents (type=".txt").
Newline characters are ignored in HTML.
If you want new lines in HTML, you must use paragraphs or <br>:
Examples
document.write("<br>");
document.write("Have a nice day!");
document.write("<p>Have a nice day!</p>");
Browser Support
document.write is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera | IE |
| Yes | Yes | Yes | Yes | Yes | Yes |

