Open In App

HTML onload Event Attribute

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The onload event attribute works when an object has been loaded. This attribute is mostly used within the <body> element to execute a script. It can be used with other elements as well. This attribute is used to check the visitor’s browser type and browser version and load the proper version of the web page based on the information.

In simple words, It activates when a webpage or an element has finished loading and is also essential for executing scripts or functions once the content is fully rendered. It is commonly used to initialize interactive features, fetch data, or manipulate the DOM.

Syntax: 

<element onload = "script">

Supported Tags: 

Attribute Value:

This attribute contains a is single value script and it runs when onload event is triggered. This attribute is associated with many HTML elements.

Example 1:

In this example, we will see the implementation of onload tag.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>onload event attribute</title>
    <script>
        function Function() {
            alert("Page is loaded");
        }
    </script>
    <style>
        body {
            text-align: center;
        }
 
        h1 {
            color: green;
        }
    </style>
</head>
 
<body onload="Function()">
    <h1>
        GeeksforGeeks
    </h1>
    <h2>
        onload event attribute
    </h2>
</body>
 
</html>


Output: 

Example 2: 

In this example, we will see the implementation of onload tag with another example.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>onload event attribute</title>
</head>
 
<body>
    <img src=
         onload="loadImage()"
         width="500"
         height="132">
    <script>
        function loadImage() {
            alert("Image loaded successfully");
        }
    </script>
</body>
 
</html>


Output: 

Supported Browsers:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Opera 4 and above
  • Safari 1.3 and above


Last Updated : 21 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads