The write()
method outputs content to a document.
document.write(exp1,exp2,exp3,...)
Parameter | Description |
---|---|
exp1,exp2,exp3,... | Optional. What to write to the output stream. |
write |
Yes | Yes | Yes | Yes | Yes |
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:
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:
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: