JavaScript Resource Events


1. load: This event occurs when a resource and its dependent resources have finished loading.

Syntax:

    <element onload=”functionName()”>


Example:

    <body onload=”myScript()”>
    <script>
        function myScript()
        {
            alert(“The page is loaded successfully”);
        }
    </script>        


2. unload: This event occurs when the document or a child resource is being unloaded i.e. when we navigates away from the page.

Syntax:

    <element onunload=”functionName()”>


Example:

    <body onunload=”myScript()”>
    <script>
        function myScript()
        {
            alert(“Thank You for visiting TutorialsDuniya.com”);
        }
    </script>        



3. error: This event occurs when an error occurs while loading an external file (e.g. a document or an image).

Syntax:

    <element onerror=”functionName()”>


Example:

    <img src=”Logo.jpg” onerror=”myScript()” />
    <script>
        function myScript()
        {
            alert(“Logo.jpg image can not be loaded”);
        }
    </script>    


4. abort: The abort event is fired when the loading of a resource has been aborted.

Syntax:

    <element onerror=”functionName()”>


Example:

    <video src=”TutorialsDuniya.mp4” onabort=”myScript()” />
    <script>
        function myScript()
        {
            alert(“Video is aborted”);
        }
    </script>