How to Call a JavaScript Function on Page Load
Calling a JavaScript function on page load is a fundamental technique used in web development to execute specific code as soon as a web page finishes loading. This capability allows developers to initialize variables, set up event listeners, manipulate the DOM, fetch data from APIs, or perform any other desired actions when the page is ready. There are various methods to achieve this, including using the onload
attribute in the <body>
tag to specify an inline event handler, utilizing the window.onload
event, or listening for the DOMContentLoaded
event. By employing these techniques, developers can ensure that their JavaScript functions are automatically triggered when the page is loaded, enhancing the interactivity and functionality of their web applications.
Table Of Contents
01. Inline Event Handler:
You can use the onload
attribute in the <body>
tag to specify a function that should be executed when the page finishes loading. Here's an example:
<!DOCTYPE html> <html> <head> <title>How to Call a JavaScript Function on Page Load</title> </head> <body onload="functionOnPageLoad()"> <!-- Rest of HTML content --> <script> function functionOnPageLoad() { alert("This is working."); } </script> </body> </html>
when the page finishes loading, the functionOnPageLoad()
the function will be called.
Read Also:
- Announcement Popup Box Using PopboxJs | Rustcode
- Cursor Animation With Hover Effect Using GSAP | HTML, CSS And GSAP
- Custom Mouse Cursor Javascript | HTML, CSS And PointerJs
- Html Elements Smooth Drag And Drop Animation | HTML, CSS And Sortable
- Particle Background Animation | HTML, CSS And ParticleJs
- Portfolio Landing Page With Animation And Responsiveness | HTML, CSS, jQuery And GSAP
- Two Image Slider | HTML, CSS And JsPlugin
02. Window "onload" Event:
You can use the window.onload
event handler in JavaScript to call a function when the entire page, including all its resources, finishes loading. In this case, when the page has finished loading, the function specified inside window.onload
will be executed. Here's an example:
<!DOCTYPE html> <html> <head> <title>How to Call a JavaScript Function on Page Load</title> </head> <body> <!-- Rest of HTML content --> <script> window.onload = function() { alert("This is working."); } </script> </body> </html>
Read Also:
03. "DOMContentLoaded" Event:
If you only need to ensure that the DOM (Document Object Model) is ready, rather than waiting for all resources to load, you can use the DOMContentLoaded
event. It triggers when the HTML is parsed and the DOM is ready to be manipulated. The function inside document.addEventListener
will be called when the DOM is ready. Here's an example:
<!DOCTYPE html> <html> <head> <title>How to Call a JavaScript Function on Page Load</title> </head> <body> <!-- Rest of HTML content --> <script> document.addEventListener('DOMContentLoaded', function() { alert("This is working."); }); </script> </body> </html>
04. Multiple Function Calling:
If you want to execute multiple functions on page load using the inline event handler approach, you can separate them with semicolons (;) inside the onload attribute. Here's an example:
<!DOCTYPE html> <html> <head> <title>How to Call a JavaScript Function on Page Load</title> </head> <body onload="loadFunction1();loadFunction2();"> <!-- Rest of HTML content --> <script> function loadFunction1() { alert("loadFunction1 is working."); }; function loadFunction2() { alert("loadFunction2 is working."); }; <!-- alert("This is working."); --> </script> </body> </html>
Read Also:
- Preloader Loading Animation | Html, Css And Gsap
- Social Media Buttons Animation With Rotation Hover Effect | Html & Css
- Pure CSS Progress Bar Animation | HTML And CSS
- Shining Effect On Text | HTML And CSS
- Show And Hide Password Animation | HTML, CSS And Javascript
- Simple Button Group Design | HTML And CSS
- Responsive Newsletter Subscription Form Design | HTML And CSS
- Responsive Price Table Design | HTML And CSS
Comments
Post a Comment