Name
Window.onload — the handler invoked when a document finishes loading
Availability
JavaScript 1.0
Synopsis
<body onload=”handler
" ... >
<frameset onload=”handler
" ... >
window
.onload
Description
The onload
property of a Window specifies an event
handler function that is invoked when a document or frameset is
completely loaded into its window or frame.
The
initial value of this property is a function that contains the
semicolon-separated JavaScript statements specified by the
onload
attribute of the
<body>
or
<frameset>
tags.
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.
Usage
If any of your document’s event handlers depend on the document
being fully loaded, you should 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, the user would get
unintended behavior or an error message after clicking the button.
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.
See Also
Get JavaScript: The Definitive Guide, Fourth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.