HTML DOM Document write()

Write some HTML elements directly to the HTML output:

document.write("

Hello World!

Have a nice day!

");

Using document.write() after a document is loaded, deletes all existing HTML:

// This should be avoided:
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

document.write(exp1, exp2, . expN)

Parameters

Parameter Description
exp1. Optional.
The output stream.
Multiple arguments are appended to the document in order of occurrence.

Return Value

More Examples

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();
document.write("

Hello World

");
document.close();

Open a new window and write some HTML into it:

const myWindow = window.open();
myWindow.document.write("

New Window

");
myWindow.document.write("

Hello World!

");

The Difference Between write() and writeln()

The writeln( ) method is only useful when writing to text documents (type=".txt").

Example

document.write("Hello World!");
document.write("Have a nice day!");
document.write("
");
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
:

Examples

document.write("Hello World!");
document.write("
");
document.write("Have a nice day!");
document.write("

Hello World!

");
document.write("

Have a nice day!

");

Browser Support

document.write is supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes
W3schools Pathfinder Track your progress - it's free!

Get Certified

COLOR PICKER

colorpicker

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

Top Tutorials
Top References
Top Examples
Get Certified

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.