HTML | onload Event Attribute

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Supported Browsers: The browser supported by onload attribute are listed below:

  • Chrome
  • Internet Explorer
  • Firefox
  • Safari
  • Opera


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.