JavaScript: 3 Ways to Use document.write

The document.write command is a standard, yet powerful, JavaScript command for writing output to a page. In this article I’ve outline 3 ways to use document.write statements.

Example 1

<script type="text/javascript">
document.write('	<h4>Example 1 using document.write</h4>');
document.write('	<p>This example uses multiple document.write commands to write ouput to a page.</p>');

Example 2

<script type="text/javascript">
var output = '';
output  = '<div>';
output  = '	<h4>Example 2 using document.write</h4>';
output  = '	<p>This example uses a lengthy string and a single document.write command to write ouput to a page.</p>';
output  = '</div>';

Example 3

<script type="text/javascript">
var output = [
'	<h4>Example 3 using document.write</h4>',
'	<p>This example uses an array and a single document.write command to write output to a page.</p>',

View live demonstration with all 3 examples.

One thought on “JavaScript: 3 Ways to Use document.write”

Leave a Reply

Your email address will not be published. Required fields are marked *