Traditionally, the onload attribute on the body element was used to execute a JavaScript function when the page fully loaded. However, some frameworks like JSP fragments don't provide a body element to add the onload attribute. This raises the question: Is there an alternative way to call a JavaScript function upon page load without using jQuery?
The Solution: Anonymous Function onload
Instead of directly assigning the JavaScript function to the onload attribute, you can bind it to an anonymous function. This anonymous function can then be invoked when the page loads.
window.onload = function() { yourFunction(param1, param2); };
This approach allows you to pass parameters to the intended function, potentially making it more versatile. You can also execute multiple functions within the anonymous function if needed.
Remember, this solution relies on the window.onload event listener to execute the JavaScript code once the page has loaded completely. It's a convenient and flexible alternative to the onload attribute when the body element is unavailable.
The above is the detailed content of How to Call a JavaScript Function on Page Load Without the `onload` Attribute?. For more information, please follow other related articles on the PHP Chinese website!