
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
Execute a script when the element is finished loading in HTML?
In this article we are going to learn about execute a script when the element is finished loading in HTML.
When an object is loaded, the onload event takes place.Onload is most frequently used in the <body> element to run a script after a web page has loaded all of its content completely. This can be utilised for a number of things, such as checking for cookies or setting the appropriate version of the page based on the user's browser.
Let’s dive into the article to understand more about executing a script when the element is finished loading in HTML.
Using Onload Method
After the element has finished loading, the onload event takes place. To execute a script after the webpage has fully loaded, use this together with the body element.
Syntax
Following is the syntax for onload method.
<body onload="functionToBeExecuted">
Example
In the following example we are using onload method.
<!DOCTYPE html> <html> <body onload="loadFunction()"> <h1>Welcome To TutorialsPoint</h1> <script> function loadFunction() { alert("Loaded Successfully"); } </script> </body> </html>
When the script gets executed, the event gets triggered and displays an alert "loaded successfully" on the webpage.
If a user clicks on "OK", the page gets loaded successfully and displays the text "welcome to tutorialspoint".
Using window.onload Method
The window element's onload property is used to execute a script once the webpage has fully loaded. Once the webpage has loaded, the function is activated by the onload property.
Syntax
Following is the syntax for window.onload method
window.onload = function exampleFunction() { // Function to be executed }
Example
Consider the following example we are using window.onload method.
<!DOCTYPE html> <html> <body> <h1>The Best E-Way Learning.</h1> <script> function loadFunction() { alert("Window Loaded Successfully"); } window.onload = loadFunction(); </script> </body> </html>
On running the above script, the output window pops up, displaying the alert "windows loaded successfully" when the event gets triggered on the webpage.
When the users click OK, the window gets loaded successfully and displays the text "the best e-way learning" on the webpage.
- Related Articles
- Execute a script when the element is invalid in HTML?
- How to execute the script when the page has finished parsing in HTML?
- Execute a script when the element is being dragged in HTML?
- Execute a script when the element is being clicked in HTML?
- Execute a script when the element loses focus in HTML?
- Execute a script when the element gets focus in HTML?
- Execute a script when the element is being double-clicked in HTML?
- Execute a script when the dragged element is being dropped in HTML?
- Execute a script when the element gets user input in HTML?
- Execute a script when the cue changes in a element in HTML?
- Execute a script when the content of the element is being cut in HTML?
- Execute a script when the content of the element is being copied in HTML?
- Execute a script when a element is shown as a context menu in HTML?
- Execute a script when the user opens or closes the element in HTML?
- Execute a script when the file is unavailable in HTML?
