JavaScript! window.onload = someFunction and window.onload = someFunction()

javascript

Solution

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)
}

Problem

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?

Original source