Is there a difference between:
window.onload = someFunction;
window.onload = someFunction();
The parentheses at the end. Do they make any difference?
We generally use the first one! What if we Have to pass some parameter to the function. How will we do it by using the first statement?
As explained otherwise, the first form
window.onload = someFunction
Simply set the "onload" variable to be equals to the "someFunction" function ; when the page finishes loading, this function is called.
The other form :
window.onload = someFunction()
Sets the "onload" variable to be the result of calling someFunction. Unless "someFunction" itself returns a function, this is probably not what you want to do.
By default, the onload function is called with a single "event" argument. If you want to pass arguments, you might be able to do something like this :
window.onload = function (event) {
someFunction(someArg, someOtherArg)
}
Your second statement assigns the result of someFunction() to window.onload.
If you want to add a parameter, you can do the following:
window.onload = function () {
someFunction(parameter);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With