Javascript Reference - HTML DOM write() Method








The write() method outputs content to a document.

Syntax

document.write(exp1,exp2,exp3,...)

Parameter

Parameter Description
exp1,exp2,exp3,... Optional. What to write to the output stream.

Browser Support

write Yes Yes Yes Yes Yes




Example 1

The following code shows how to Write some text to the output stream:


<!DOCTYPE html>
<html>
<body>
<!--from ww  w.ja  v  a 2s  . c  om-->
<script>
document.write("Hello World!");
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to output formatted text to the output stream.


<!DOCTYPE html>
<html>
<body>
<!--from w  w w.ja  va  2  s .  c  om-->
<script>
document.write("<h1>A</h1><p>this is a test</p>");
</script>

</body>
</html>

The code above is rendered as follows:

Example 3

The following code shows how to use write() and writeln().


<!DOCTYPE html>
<html>
<body>
<pre>
<script>
    document.write("A");
    document.write("B");
</script><!--from www  . j  a  v a  2s .com-->
</pre>

<pre>
<script>
    document.writeln("C");
    document.writeln("D");
</script>
</pre>

</body>
</html>

The code above is rendered as follows: