Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
You are here: Reference > JavaScript > client-side > event handling > events > onload

onload event | load event

Browser support:
Occurs when an object has been loaded.
The onload events for the body, window and document (only in Opera before version 10.5) objects are the same. They fire after all objects in the DOM hierarchy (images, sub-frames, ...) have finished loading and the document object has been built up. At that point, you can access the elements of the document (such as getElementById, getElementsByName), you can manipulate the contents of the document, etc.

How to register:

In HTML:
<ELEMENT onload="handler">

In JavaScript:
object.onload = handler;
object.addEventListener ("load", handler, useCapture);
9
object.attachEvent ("onload", handler);
You can find the related objects in the Supported by objects section below.
The event object is accessible to all event handlers in all browsers. The properties of the event object contain additional information about the current event. To get further details about these properties and the possible event handler registration methods, please see the page for the event object.
For a complete list of events, see the page for Events in JavaScript.

Basic information:

Bubbles No
Cancelable No
Event object -

Actions that invoke the onload event:

  • If the document or an element has been loaded.

Events related to the onload event:

Example HTML code 1:

This example illustrates the use of the onload event for the body element:
<head>
    <script type="text/javascript">
        function OnLoadDocument () {
            alert ("The document has been loaded.");
            document.body.style.backgroundColor = "red";
        }
    </script>
</head>
<body onload="OnLoadDocument ();">
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example illustrates the use of the onload event for img elements:
<head>
    <script type="text/javascript">
            // Internet Explorer specific
        function OnAbortImage () {
            var info = document.getElementById ("info");
            info.innerHTML += "<br />The loading of the image has been aborted.";
            RemoveEsc ();
        }

        function OnLoadImage () {
            var info = document.getElementById ("info");
            info.innerHTML += "<br />The image has been loaded.";
            RemoveEsc ();
        }

            // Internet Explorer specific
        function OnStateChangeImage (image) {
            var info = document.getElementById ("info");
            info.innerHTML += "<br />readyState: " + image.readyState;
        }

        function RemoveEsc () {
            var esc = document.getElementById ("esc");
            esc.parentNode.removeChild (esc);
        }
    </script>
</head>
<body>
    <span id="info" style="color:red">The image is loading.</span>
    <br /><br />
    <span id="esc">Press the Escape key to abort the process.</span>
    <br /><br />
    <img src="large.bmp" width="200px" height="150px" 
        onabort="OnAbortImage ()" onload="OnLoadImage ()" onreadystatechange="OnStateChangeImage (this)" />
</body>
Did you find this example helpful? yes no

Example HTML code 3:

This example illustrates the use of the onload event for XMLDocument objects. Note that this example does not work in Google Chrome and Safari, because they do not support the onload event for XMLDocument objects.
Code
ajax.js
<head>
    <script type="text/javascript" src="ajax.js"></script>
    
    <script type="text/javascript">
        var xmlDoc = null;

        function LoadXML () {
            xmlDoc = CreateXMLDocumentObject ();    // defined in ajax.js
            if (!xmlDoc) {
                return;
            }

            var url = "large.xml";
            xmlDoc.async = true;

            if (xmlDoc.addEventListener) {  // Firefox, Opera, Google Chrome and Safari
                xmlDoc.addEventListener("load", OnLoadXML, false);
            }
            else {
                xmlDoc.onreadystatechange = OnStateChange;
            }

            xmlDoc.load (url);
        }

        function OnLoadXML () {
            LoadingFinished ();
        }

        function OnStateChange () {
            var output = document.getElementById ("output");
            switch (xmlDoc.readyState) {
            case 1:
                output.innerHTML += "The load method was called, but no data has been received yet.<br/>";
                break;
            case 2:
                output.innerHTML += "A part of the data has been loaded, but the document object model (DOM) is not yet available.<br/>";
                break;
            case 3:
                output.innerHTML += "The document object model has been built from the received data and it is available.<br/>";
                break;
            case 4:
                LoadingFinished ();
                break;
            };
        }

        function LoadingFinished () {
            var output = document.getElementById ("output");
            output.innerHTML += "All data has been loaded, the operation is finished:<br/>";

            var errorMsg = null;
            if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
                errorMsg = "XML Parsing Error: <span style='color:red'>" + xmlDoc.parseError.reason
                          + " at line " + xmlDoc.parseError.line
                          + " at position " + xmlDoc.parseError.linepos
                          + "</span><br/>";
            }
            else {
                if (xmlDoc.documentElement) {
                    if (xmlDoc.documentElement.nodeName == "parsererror") {
                        errorMsg = "<span style='color:red'>" + xmlDoc.documentElement.childNodes[0].nodeValue + "</span><br/>";
                    }
                }
            }
            if (errorMsg) {
                output.innerHTML += errorMsg;
            }
            else {
                output.innerHTML += "<span style='color:blue'>The XML is valid.</span><br/>";
            }
        }
    </script>
</head>
<body>
    <button onclick="LoadXML ()">Analyze the loading process of an XML</button>
    <br/><br/>
    <div id="output" style="width:420px; height:200px; overflow:auto; border:1px solid #000000; background-color:#f5e4b1;"></div>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content