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('<div>');
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>');
document.write('</div>');
</script>

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>';
document.write(output);
</script>

Example 3

<script type="text/javascript">
var output = [
'<div>',
'	<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>',
'</div>'
];
document.write(output.join('\n'));
</script>

View live demonstration with all 3 examples.

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

Comments are closed.