- How do I call a JavaScript function on page load?
- Using the onload event in the tag
- Syntax
- Example
- Methods to call a JavaScript function on page load.
- Call the JavaScript function on page load by using onload event in the HTML body tag.
- Using the window.onload Event in JavaScript
- Syntax
- Example
- Methods to call a JavaScript function on page load.
- Call the JavaScript function on page load by using window.onload event in the JavaScript.
- Using the DOMContentLoaded Event
- Syntax
- Example
- Methods to call a JavaScript function on page load.
- Call the JavaScript function on page load by using document.addEventListener( ) method.
- Conclusion
- Call javascript content page
- All replies
- Welcome to ASP.NET!
- Welcome to ASP.NET!
- Call JavaScript function from ASP.NET Content Page
- About The Author
- Call javaScript Function After Whole Page Load Complete
- 1. First Simple Way
- Welcome to tutsmake.com
- 2. Second Way
- Welcome to tutsmake.com
How do I call a JavaScript function on page load?
This tutorial will teach us to call a JavaScript function on page load. In many cases, while programming with HTML and JavaScript, programmers need to call a function, while loading the web page or after the web page load finishes. For example, programmers need to show the welcome message to the user on page load, and the programmer needs to call a JavaScript function after the page load event.
There are various ways to call a function on page load or after page load in JavaScript, and we will look at them one by one in this tutorial.
- Using the onload event in the tag
- Using the window.onload Event
- Using the DOMContentloaded Event
Using the onload event in the tag
The first approach for calling a function on the page load is the use an onload event inside the HTML 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.
Syntax
Follow the below syntax to use onload event in the tag.
Example
In the below example, we have created the JavaScript function named welcomeFunction(). We are calling that function from the tag using the onload event, and it shows the welcome message in the alert box to the users.
Methods to call a JavaScript function on page load.
Call the JavaScript function on page load by using onload event in the HTML body tag.
let message = document.getElementById("message"); function welcomeFunction()