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.

JoshStauffer.com runs on the Genesis Framework

Genesis Framework

Genesis empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go. It's that simple - start using Genesis now!

Take advantage of the 6 default layout options, comprehensive SEO settings, rock-solid security, flexible theme options, cool custom widgets, custom design hooks, and a huge selection of child themes ("skins") that make your site look the way you want it to. With automatic theme updates and world-class support included, Genesis is the smart choice for your WordPress website or blog.

Become a StudioPress affiliate

Comments

  1. Sumon says:

    Great Tricks, Thanks

Speak Your Mind

*