Write some HTML elements directly to the HTML output:
document.write("Have a nice day!
");Using document.write() after a document is loaded, deletes all existing HTML:
// This should be avoided:More examples below.
The write() method writes directly to an open (HTML) document stream.
The write() method deletes all existing HTML when used on a loaded document.
The write() method cannot be used in XHTML or XML.
The write() method is most often used to write to output streams opened by the the open() method.
Parameter | Description |
exp1. | Optional. The output stream. Multiple arguments are appended to the document in order of occurrence. |
Write a date object directly to the HTML ouput:
document.write(Date());Open an output stream, add some HTML, then close the output stream:
document.open();Open a new window and write some HTML into it:
const myWindow = window.open();Hello World!
");The writeln( ) method is only useful when writing to text documents (type=".txt").
document.write("Hello World!");
document.write("Have a nice day!");
document.write("
");
document.writeln("Hello World!");
document.writeln("Have a nice day!");
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
:
Hello World!
");Have a nice day!
");document.write is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com
If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.