home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference

Window.onload() Handler


Window.onload() Handler---executed when a document finishes loading


Navigator 2.0, Internet Explorer 3.0


<BODY    a definition of the handler
    [ onLoad="JavaScript statements" ]
<FRAMESET    another way to define the handler
    [ onLoad="JavaScript statements" ]
window.onload=handler-func  defining the handler directly
window.onload();  an explicit invocation of the handler


onload() is an event handler invoked by the browser when a document or frameset is completely loaded into the browser. The onload() handler is defined by the onLoad attribute of the <BODY> or <FRAMESET> HTML tags. The value of this attribute may be any number of JavaScript statements, separated by semicolons.

When the onload() event handler is invoked, you can be certain that the document has fully loaded, and therefore that all scripts within the document have executed, all functions within scripts are defined, and all forms and other document elements have been parsed and are available through the Document object.


If any of your document's event handlers depend on the document being fully loaded, you should be sure to check that it is loaded before executing those handlers. If the network connection were to stall out after a button appeared in the document, but before the parts of the document that the button relied on were loaded, then when the user clicks the button they will get unintended behavior or an error message. One good way to verify that the document is loaded is to use the onload() handler to set a variable, loaded, for example, to true, and to check the value of this variable before doing anything that depends on the complete document being loaded.


JavaScript is supposed to guarantee that the onload() handler for each frame in a window will be invoked before the onload() handler for the window itself. Unfortunately, Navigator 2.0 does not always do this. Thus in a framed document, you may need to check that each of your individual frames has fully loaded.

Previous Home Next
Window.onfocus() Book Index Window.onunload()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell