HTML | DOM onload Event
The DOM onload event in HTML occurs when an object has been loaded. The onload event is mostly used within the element, The <body>
The onload event can be used to check the browser type and browser version, and load the version of the web page based on the information. The onload event can also be used for cookies.
Supported tags
- <body>
- <frame>
- <iframe>
- <img>
- <input type=”image”>
- <link>
- <script>
- , <style>
Syntax:
- In HTML:
<element onload="myScript">
- In JavaScript:
object.onload = function(){myScript};- In JavaScript, using the addEventListener() method:
object.addEventListener("load", myScript);Example: using the addEventListener() method
html
<!DOCTYPE html><html><head> <title> HTML DOM onload Event </title></head><body> <center> <img src= id="imgid"> <p id="pid"></p> <script> document.getElementById( "imgid").addEventListener("load", GFGFun); function GFGFun() { document.getElementById("pid").innerHTML = "Image loaded"; } </script> </center></body></html> |
Output:

Supported Browsers: The browsers supported by HTML DOM onload Event are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Apple Safari
- Opera
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.


