user2340213
user2340213

Reputation: 83

difference between window.onload=function_name and window.onload=function_name()

When I used window.onload=function_name() where function_name is a java script function, the function did not get executed. But it works fine when I use window.onload=function_name.

Upvotes: 1

Views: 192

Answers (2)

user1646111
user1646111

Reputation:

window.onload=function_name;

This will be executed as expected when windows loading finished, while this one:

window.onload=function_name();

Will be executed when browser reaches that point and the value for the function will be returned to the window.load after page loaded.

Upvotes: 1

kol
kol

Reputation: 28688

window.onload=function_name() calls the function function_name and sets the onload handler to its return value.

window.onload=function_name sets the handler to the function itself, so function_name will be called when the onload event occurs.

Upvotes: 3

Related Questions