This problem also bit me. I think it might be dangerous to add the "load" callback to the end after the page loads, since it will not be called at all if the "load" event has already been fired!
I completely agree - I also expected that my callback would be called immediately after the event that it should have been waiting for was already completed. It looks like this will be more consistent with how the event โreadyโ for the document immediately launches its callbacks if the document is already ready ...
Here is my workaround written in CoffeeScript:
$(window).on 'load', -> window._loaded = true window.on_window_load_safe = (callback) -> if window._loaded
You can check it something like this:
setTimeout(-> on_window_load_safe -> console.debug 'on_window_load_safe called me' , 2000)
source share