ded/script.js

window onload event not getting fired

Closed this issue · 2 comments

At the moment I am loading a simple jquery based script the following way:

$script.order([
  '//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js',
  '/public/scripts/app.min.js'
]);

This is working fine but when trying to catch the onload event on the window in my app script it is not getting fired.

Example:

$(window).on('load', function(){
  console.log('Loaded');
});

This event is not getting fired on the latest chrome version (47.0.2526.111). When I'm loading jquery and the app script blocking I'm getting the event fired correctly.

Is there a way to load it asynchronously while getting the event?

Hey @DenisMir, $script is an async script loader and sometimes due to the nature of how scripts are loaded (asynchronously), some scripts containing registration of key events like 'load' might not fire if that script is loaded after the browser has gone past page load (this is when the load event is fired). So, jquery onload will not capture the event if the script is loaded well after the browser fired the event (which is a big possibility since the script is loaded asynchronously) because the binding/registration happens late. So, you need something that covers late-binding to the event. You can use jqery on ready instead and you should be fine. Plus, the $script.order API is fine.

You are right. I figured that out as well. The key is late-binding just checking out the document.readyState (https://developer.mozilla.org/de/docs/Web/API/Document/readyState).