Open In App

HTML DOM onload Event

The HTML DOM onload event in HTML occurs when an object has been loaded. The onload event is mostly used within the <body> tag, in order to run the script on the web page that will load all the content completely. The onload event can be used to check the user’s browser type and browser version and load the version of the web page based on the information. The onload event can also be used for cookies. 

Syntax:



In HTML:

<element onload="scriptFile">

In JavaScript:



object.onload = function(){scriptFile};

In JavaScript, using the addEventListener() method:

object.addEventListener("load", scriptFile);

Example: Using the addEventListener() method




<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML DOM onload Event
    </title>
</head>
 
<body>
    <img src=
         id="imgid" alt="GFG_logo">
    <p id="pid"></p>
 
    <script>
        document.getElementById("imgid")
        .addEventListener("load", GFGFun);
        function GFGFun() {
            document.getElementById("pid")
            .innerHTML = "Image loaded";
        }
    </script>
</body>
 
</html>

Output: Here, the getElementById() method will return the elements that have given an ID which is passed to the function. It can also be used to change the value of any particular element or get a particular element. The DOM innerHTML property will be used to set or return the HTML content of an element.

HTML DOM onload Event

We have a complete list of HTML DOM methods, to check those please go through this HTML DOM Object Complete reference article.

Supported tags:

Supported Browsers: The browsers supported by HTML DOM onload Event are listed below:

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


Article Tags :