View Single Post
Old 06-09-2012, 12:17 AM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,529
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by iBall View Post
<offTopic style="Tip">

In that case using document.write() is fine for this purpose to help students quickly get output to the browser while learning the basics of javascript.

As soon as you start learning more about the Document Object Model (DOM), you really should then use more appropriate DOM methods to output to the browser.

document.write outputs to the current page only while it is still loading. If called after the the page has loaded, it will output to a new page. document.write is really only useful for writing to child pages.

</offTopic>
Using innerHTML is far more straightforward and less error prone for beginners to use than document.write is - there's no reason for using the more complicated document.write rather than innerHTML except in those advanced userscripts where document.write is the only way that works.

There are far fewer situations where innerHTML will break the web page functionality compared to the ways that document.write can break the functionality.

Of course the way that always works is to use the proper DOM commands that make up about 60% of JavaScript but learning those is a bit more complex and since innerHTML will work most of the time it is the obvious choice for beginners to use first.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote