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
The write()
method writes directly to an open (HTML) document stream.
Warning
The write()
method deletes all existing HTML when used on a loaded document.
The write()
method cannot be used in XHTML or XML.
Note
The write()
method is most often used to write to output streams opened by the
the open()
method.
See Also:
Syntax
Parameters
The output stream.
Multiple arguments are appended to the document in order of occurrence.
Return Value
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 use writeln() 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 |