DHTML Events
Fires immediately after the browser loads the object.
Inline HTML |
<element onload = "handler" ... > |
All platforms |
Event property |
object.onload = handler |
ECMA-262 Language Specification |
Named script |
<script FOR=object EVENT=onload> |
Internet Explorer® only |
Bubbles |
No |
Cancels |
No |
To invoke |
Open a page in the browser to invoke this event for the document or any object within it. |
Default action |
Loads the object for which the event is specified. |
A load event occurs when the user agent finishes loading all content within the document's BODY, all frames within a FRAMESET, or an OBJECT element.
The onload attribute of the BODY object sets an onload event handler for the window. This technique of calling the window onload event through the BODY object is overriden by any other means of invoking the window onload event, provided the handlers are in the same script language.
In Internet Explorer®, the onload event can be applied to the following objects as well: APPLET, EMBED, and IMG.
As the browser loads applications, embedded objects, and images as soon as it encounters the objects during parsing, the onload event for these objects occurs before the browser parses any subsequent objects. To ensure that an event handler receives the onload event for these objects, the SCRIPT object that defines the event handler must be placed before the object and the onload attribute in the object used to set the handler.
While event handlers in the Document Object Model do not receive parameters directly, the handler can query the event object for data.
Event Object Properties
clientX |
Retrieves the x-coordinate of the position of the cursor when the mouse is clicked, relative to the size of the client area of the window but excluding window decorations or scroll bars. |
clientX |
Returns the y-coordinate of the position of the cursor when the mouse is clicked, relative to the size of the client area of the window but excluding window decorations or scroll bars. |
offsetX |
Retrieves the horizontal coordinate of the mouse's position relative to the object firing the event. |
offsetX |
Retrieves the vertical coordinate of the mouse's position relative to the object firing the event. |
screenX |
Retrieves the horizontal position of the mouse, in pixels, relative to the user's screen. |
screenY |
Retrieves the vertical position of the mouse, in pixels, relative to the user's screen. |
type |
Retrieves the event name from the event object. |
x |
Returns the horizontal position of the mouse when the event fires. |
y |
Returns the vertical position of the mouse when the event fires. |
This simple example demonstrates use of the onload event to notify when a page has loaded.
<script for=window event=onload>
alert("The page has loaded.");
</script>
Show me
The following example shows use of the onload event to trigger a function as the page loads.
Show me
APPLET, EMBED, FRAMESET, IMG, LINK, OBJECT, SCRIPT, STYLE, window
onreadyStatechange, onunload