How to run a function when the page is loaded in JavaScript ? Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report A function can be executed when the page loads successfully. This can be used for various purposes like checking for cookies or setting the correct version of the page depending on the user's browser. Below are the approaches to run a function when the page is loaded in JavaScript: Table of Content Using onload methodUsing window.onloadMethod 1: Using onload method: The body of a webpage contains the actual content that is to be displayed. The onload event occurs whenever the element has finished loading. This can be used with the body element to execute a script after the webpage has completely loaded. The function that is required to be executed is given here. Syntax:<body onload="functionToBeExecuted">Example: In this example, we have used onload method. html <!DOCTYPE html> <html> <head> <title> How to run a function when the page is loaded in javascript ? </title> </head> <body onload="console.log('The Script will load now.')"> <h1 style="color: green">GeeksforGeeks</h1> <b> How to run a function when the page is loaded in javascript ? </b> <p> The script has been executed. Check the console for the output. </p> </body> </html> Output: Console Output: Method 2: Using window.onloadThe window object represents the browser window. The onload property processes load events after the element has finished loading. This is used with the window element to execute a script after the webpage has completely loaded. The function that is required to be executed is assigned as the handler function to this property. It will run the function as soon as the webpage has been loaded. Syntax:window.onload = function exampleFunction(){// function to be executed}Example: In this example, we have used window.onload html <!DOCTYPE html> <html> <head> <title> How to run a function when the page is loaded in javascript ? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to run a function when the page is loaded in javascript? </b> <p> The script has been executed. Check the console for the output. </p> <script> window.onload = function exampleFunction() { console.log('The Script will load now.'); } </script> </body> </html> Output: Console Output: JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples. Comment More infoAdvertise with us Next Article How to Call a Vue.js Function on Page Load ? S sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to check whether the background image is loaded or not using JavaScript ? In this article, we will check whether the background image is loaded or not using JavaScript. In JavaScript, onload event is used to check whether a window is loaded or not. Similarly, we can use that event to check whether a particular element has loaded or not. There are two ways in which we can 2 min read How to Execute JavaScript After Page Load? When a webpage loads, it takes time. The browser reads the HTML, builds the Document Object Model (DOM), and starts rendering the page. If your JavaScript runs too early, it might try to change elements that aren't ready yet, causing errors. Running JavaScript after the page loads makes sure all the 5 min read How to Call a Vue.js Function on Page Load ? In this article, we will learn how to call a vue.js function on page load in VueJS. We will explore two different approaches with practical implementation in terms of examples and outputs. Table of Content Using mounted HookUsing created HookUsing mounted HookIn this approach, are using the mounted 2 min read How to reload page only once in JavaScript ? Reloading a webpage once using JavaScript ensures that users see the most up-to-date content on their initial visit. This technique leverages session storage to track if the page has been reloaded, enhancing user experience by preventing unnecessary reloads and ensuring content freshness. Using Loca 3 min read How to check a webpage is loaded inside an iframe or into the browser window using JavaScript? An iFrame is a rectangular frame or region in the webpage to load or display another separate webpage or document inside it. So basically, an iFrame is used to display a webpage within a webpage. You can see more about iFrames here : HTML iFrames There may be a variety of reasons for checking whethe 4 min read How to check a webpage is loaded inside an iframe or into the browser window using JavaScript? An iFrame is a rectangular frame or region in the webpage to load or display another separate webpage or document inside it. So basically, an iFrame is used to display a webpage within a webpage. You can see more about iFrames here : HTML iFrames There may be a variety of reasons for checking whethe 4 min read Like