I’m binding the window.onload event like this
// It's a little more complex than this, I analyze if there is any other function
// attached but for the sake of the question it's ok, this behaves the same.
window.onload = myfunction;
Onload is triggered twice on my local machine a several times on the production server
If I change it by the jQuery equivalent
$jQuery(window).load(myfunction);
It behaves as expected (executed only once).
Could you help me to understand possible reasons why the first option it’s not working as supposed?
Thanks!
The parentheses on your assignment —myfunction()— executes your function. You haven’t shown whatmyfunctiondoes, but this means that the return value from that function is being assigned towindow.onload, not the function itself. So, I don’t know how that is getting executed, unless you have somehow got that to work, like ending the function withreturn this;You want
Given the nature of
window.onload, it seems unlikely that pure browser events alone are making both calls tomyfunction. Therefore, a breakpoint inside your function will help you see the call stack. I’ve included screenshots for Chrome.Sample code:
On the right, under “Call Stack”, you see
alertmeis called bytestsecondcall