Disclosure: Your support helps keep the site running! We earn a referral fee for some of the services we recommend on this page. Learn more Show <body onLoad=""> do?Fires a script when the page has finished loading.The Example usage
Points to be aware of
Claire Broadley Claire is seasoned technical writer, editor, and HTML enthusiast. She writes for HTML.com and runs a content agency, Red Robot Media. Summary: in this tutorial, you will learn how to handle the load event that fires on the document, image, and script elements in JavaScript. The window’s load eventFor the To handle the 0 method:
Or use the 1 property of the 2 object:
If you maintain a legacy system, you may find that the
It’s a good practice to use the 0 method to assign the 1 event handler whenever possible. The image’s load eventThe 0 method of the image elements. The following example uses the
You can assign an 1 event handler directly using the 1 attribute of the 2 element, like this:
If you create an image element dynamically, you can assign an 1 event handler before setting the 4 property as follows:
How it works:
The script’s load eventThe 8 element also supports the load event slightly different from the standard ways. The script’s load event allows you to check if a JavaScript file has been completely loaded.Unlike images, the web browser starts downloading JavaScript files only after the 4 property has been assigned and the 8 element has been added to the document. The following code loads the 3 file after the page has been completely loaded. It assigns an 1 event handler to check if the 3 has been fully loaded. What is onload in HTML?The onload attribute fires when an object has been loaded. onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.). However, it can be used on other elements as well (see "Supported HTML tags" below).
Which HTML elements support onload?onload is an event specific to the body , frame , iframe , img , link , and script elements.
Can we use onload in div?The onload event can only be used on the document(body) itself, frames, images, and scripts. In other words, it can be attached to only body and/or each external resource. The div is not an external resource and it's loaded as part of the body, so the onload event doesn't apply there.
How to call a JavaScript function onload of HTML?The first approach for calling a function on the page load is the use an onload event inside the HTML <body> tag. As you know, the HTML body contains the entire content of the web page, and when all HTML body loads on the web browser, it will call the function from the JavaScript.
|