- onload is most frequently used within the
<body>element to perform a script once a website has fully loaded (including script files, images, CSS files, etc.).
- The onload event shall be used to verify the type of the visitor's browser and version of the browser, and then load an individual web page version based on the information.
- The onload event can deal with cookies.
onload Event Explained
window.onload function is widely supported by popular browsers. It begins as soon as the resource is loaded.
Learn Event Syntax
object.onload = () => sampleScript();
The addEventListener() method is supported by Internet Explorer 9 and newer versions only.
Code Examples: Best Practice
In this example, an
<img> element is signed onload. Alert
Image is successfully loaded is displayed after an image is loaded:
<img src="https://cdn.bitdegree.org/learn/pom-laptop.png?raw=true" onload="ImageLoad()" width="120" height="120">
Now, in this example, we deal with cookies by using the onload event: