How to Call a JavaScript Function on Page Load without using jQuery

The window.onload event is very useful when you want to call a JavaScript function on page load without using jQuery. Generally, $( document ).ready() is used to execute the JavaScript code after Document Object Model (DOM) is ready. You can use the window.onload to load a function after page load using JavaScript. In the example code snippet, we will show you how to call function and execute code on page load using JavaScript.

The following code calls a JavaScript function named showDelay() on page load using window.onload event.

function showDelay(){
    //code goes here
}

// call function on page load
window.onload = showDelay;

The following code calls a JavaScript function with parameter on page load using window.onload event.

function showDelay(type){
    //code goes here
}

// call function with parameter
window.onload = function () {
    showDelay(2);
};

Leave a reply

keyboard_double_arrow_up