This article explains the purpose of the window.onload method in JavaScript by an example. For more information, refer to the Same-origin policy article. The scripts load event allows you to check if a JavaScript file has been completely loaded. Why are Suriname, Belize, and Guinea-Bissau classified as "Small Island Developing States"? directing focus ( keeping the crusher blinking ) to user input field, Date object & displaying current date and time on button click, getTime() : Date calculations by using milliseconds that is passed from January 1, 1970, Displaying Current Month using getMonth() function, Displaying present Year using getFullYear(), Showing Current Date using getDate() function, Displaying Current Day using getDay() function, Displaying Current Minutes using getMinutes() function, Displaying Current Hours using getHours() function, getUTCHours: Display UTC hour or GMT Hour, getUTCMinutes: Display UTC minutes or GMT minutes, Displaying Current Second using getSeconds() function, gettimezoneoffset() to get difference in UTC time and local time in minutes, setMinutes: Setting the minutes of a date object to fix value, setHours() Setting the minutes of a date object to fix value, setDate() Setting the date part of the date object, setMonth(): Setting the month part of date object, setFullYear(): Setting the year part of the date object, setMinutes(): Setting the minutes part of the date object, toLocaleString() Converting to local date string, toLocaleDateString() Converting to local date string, toTimeString() Converting time part of date object to a string, toUTCString to convert date object to Universal Standard String, Timer function to set time for reminder alert, Recursive timer to trigger a code block after a particular interval, Countdown script displaying days , hours , minutes and seconds left from an event, Displaying changing Clock showing date and time in a web page. Theres also a keepalive flag for doing such after-page-left requests in fetch method for generic network requests. How to put a responsive clear button inside HTML input text field ? Why do small African island nations perform better than African continental nations, considering democracy and human development? Can airtags be tracked from an iMac desktop, with no iPhone? My CSS animations with delay animation-delay attribute are getting out of synch due to the fact that CSS3 animations and transitions start immediately before document load. Examples Log a message when the page is fully loaded: window.addEventListener("load", (event) => { console.log("page is fully loaded"); }); // This is likely caused by built-in popup blockers. Post your comments , suggestion , error , requirements etc here, JavaScript Window onload to execute script as soon as page loads and difference between body onload. How to Create a Bootstrap Spinner and Display on Screen till the data from the API loads ? Use window.onload function: It is used to perform the task as soon as the page refresh or loading. The DOM tree is ready heres the event. By clicking Post Your Answer, you agree to our terms of service, privacy policy and cookie policy. In JavaScript, using the addEventListener() method: Using onload on an element. Note: The load event that is dispatched when the main document has loaded is dispatched on the window, but has two mutated properties: target is document, and path is undefined. To view the purposes they believe they have legitimate interest for, or to object to this data processing use the vendor list link below. Why do many companies reject expired SSL certificates as bugs in bug bounties? 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 following example uses the load event handler to determine if an image, which exists in the DOM tree, has been completely loaded: You can assign an onload event handler directly using the onload attribute of the
element, like this: If you create an image element dynamically, you can assign an onload event handler before setting the src property as follows: The