Skip to content
Related Articles

Related Articles

HTML | onload Event Attribute

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 16 Aug, 2022

This attribute works when an object has been loaded. This attribute 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.
Supported Tags: 

  • <body>
  • <frame>
  • <frameset>
  • <iframe>
  • <img>
  • <input type=”image”>
  • <link>
  • <script>
  • <style>

Syntax: 
 

<element onload = "script">

Attribute Value: This attribute contains single value script and it runs when onload event triggered. This attribute is associated with many HTML elements.
Example 1: 
 

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: 
 

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: The browser supported by onload attribute are listed below: 
 

  • Chrome 1
  • Edge 12
  • Internet Explorer 4
  • Firefox 1
  • Safari 1.3
  • Opera 4

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!