Skip to content
Related Articles

Related Articles

Improve Article
HTML | onload Event Attribute
  • Difficulty Level : Basic
  • Last Updated : 03 Dec, 2018

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.

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:




<!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:




<!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
  • Internet Explorer
  • Firefox
  • Safari
  • Opera



My Personal Notes arrow_drop_up
Recommended Articles
Page :