Open In App
Related Articles

HTML onload Event Attribute

Improve Article
Improve
Save Article
Save
Like Article
Like

This 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.

Supported Tags: 

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

Syntax: 

<element onload = "script">

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: 

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
  • Firefox 1
  • Safari 1.3
  • Opera 4

Last Updated : 04 Aug, 2023
Like Article
Save Article
Similar Reads
Related Tutorials