How to Call a Vue.js Function on Page Load ? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 lifecycle hook. We have defined a function as loadFn which is automatically called using a mounted hook. This hook is executed when the Vue instance is mounted to the DOM. Syntax:mounted() { // logic to call function}Example: Below is the implementation of the above-discussed approach. HTML <!DOCTYPE html> <html> <head> <title>Example 1</title> <script src= "https://cdn.jsdelivr.net/npm/vue@2"> </script> <style> body { font-family: 'Arial', sans-serif; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; } </style> </head> <body> <div id="app"> <h1 style="color: green;"> {{ message }} </h1> <h3> Approach 1: Using mounted Hook </h3> </div> <script> new Vue({ el: '#app', data: { message: 'GeeksforGeeks', }, mounted() { this.loadFn(); }, methods: { loadFn() { alert ('Page Load. Function Executed'); }, }, }); </script> </body> </html> Output: Using created HookIn this approach, we are using the created Hook to call the loadFnLog method on instance creation. We are logging the initial message. When the user clicks on the button, the message is updated and it is also been logged in the console. Syntax:created() { // logic to call load fn},Example: Below is the implementation of the above-discussed approach. HTML <!DOCTYPE html> <html> <head> <title>Example 2</title> <script src= "https://cdn.jsdelivr.net/npm/vue@2"> </script> <style> body { font-family: Arial, sans-serif; text-align: center; } #app { text-align: center; color: green; } button { padding: 10px 20px; font-size: 16px; cursor: pointer; } </style> </head> <body> <div id="app"> <h1> {{ message }} </h1> <h2> Approach 2: Using created Hook </h2> <button @click="changeMessage"> Change Message </button> </div> <script> new Vue({ el: '#app', data: { message: 'Hello, GeeksforGeeks!' }, created() { this.loadFnLog(); }, methods: { changeMessage() { this.message = 'GFG'; this.loadFnLog(); }, loadFnLog() { console.log ('Current Message:', this.message); } } }); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to run a function when the page is loaded in JavaScript ? G gauravggeeksforgeeks Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League Vue.JS Geeks Premier League 2023 +1 More Similar Reads How to Call a Function with Mouse Hover in VueJS ? In Vue.js, the function can be triggered when the element is hovered using directives like @mouseover or @mousemove, allowing for dynamic interactions based on mouse movements. Table of Content Using @mouseover DirectiveUsing @mousemove DirectiveUsing @mouseover DirectiveIn this approach, we are usi 2 min read How to set a click event once a page or view is loaded in vue.js? In this article, we will discuss how to set a click event once a page or view is loaded in vue.js. Just like every other JavaScript framework Vue also supports the Mounting hooks. Mounting hooks are often the most used hooks. They allow us to access or modify the DOM of your component just before or 2 min read How to run a function when the page is loaded in JavaScript ? 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 2 min read How to run a function when the page is loaded in JavaScript ? 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 2 min read How to Create Functional Components using Vue Loader ? Vue.js is a JavaScript framework used in building powerful and beautiful user interfaces. The key feature of Vue.js is its component-based architecture that allows the developers to create reusable and modular components. Functional components, in particular, provide a lightweight and efficient way 3 min read How to Call a Parent Method from a Child Component in Vue 3 ? Vue.js is a JavaScript framework for building user interfaces. In Vue 3 parent and child component can communicate with each other through props and events. Props are used to pass the data from the parent to the child component and events are used to pass the data from child to parent component. The 3 min read Like