onload Event

Internet Development Index

Fires immediately after the browser loads the object.

Syntax

Inline HTML<ELEMENT onload = "handler" ... > All platforms
Event propertyobject.onload = handlerJScript only
object.onload = GetRef("handler")Visual Basic Scripting Edition (VBScript) 5.0 or later only
Named script <SCRIPT FOR = object EVENT = onload> Internet Explorer only

Event Information

BubblesNo
CancelsNo
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.

Event Object Properties

Although event handlers in the DHTML Object Model do not receive parameters directly, a handler can query an event object for data.

Available Properties

altKey Sets or retrieves a value that indicates the state of the ALT key.
altLeft Sets or retrieves a value that indicates the state of the left ALT key.
clientX Sets or retrieves the x-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars.
clientY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars.
ctrlLeft Sets or retrieves the state of the left CTRL key.
offsetX Sets or retrieves the x-coordinate of the mouse pointer's position relative to the object firing the event.
offsetY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the object firing the event.
screenX Retrieves the x-coordinate of the mouse pointer's position relative to the user's screen.
screenY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the user's screen.
shiftLeft Retrieves the state of the left SHIFT key.
type Sets or retrieves the event name from the event object.
x Sets or retrieves the x-coordinate, in pixels, of the mouse pointer's position relative to a relatively positioned parent element.
y Sets or retrieves the y-coordinate, in pixels, of the mouse pointer's position relative to a relatively positioned parent element.

Remarks

The browser loads applications, embedded objects, and images as soon as it encounters the applet, embed, and img objects during parsing. Consequently, 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, place the script object that defines the event handler before the object and use the onload attribute in the object to set the handler.

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 overridden by any other means of invoking the window onload event, provided the handlers are in the same script language.

Examples

This example uses an onload event handler to display a message in the window's status bar when the page has finished loading.

<BODY>
<SCRIPT FOR=window EVENT=onload LANGUAGE="JScript">
window.status = "Page is loaded!";
</SCRIPT>
</BODY>

This example sets an onload event handler for an img object. The handler uses the event object to retrieve the URL of the image.

<SCRIPT>
function imageLoaded()
{
window.status = "Image " + event.srcElement.src + " is loaded";
}
</SCRIPT>
<BODY>
<IMG SRC="sample.gif" onload="imageLoaded()">
</BODY>

Standards Information

This event is defined in HTML 4.0 Non-Microsoft link.

Applies To

APPLET, BODY, EMBED, FRAME, FRAMESET, IFRAME, IMG, LINK, SCRIPT, window

See Also

onreadystatechange, onunload