Can anyone explain why, in Internet Explorer, code sample 1 doesn't work while code sample 2 works?
Code 1 (non-functional)
Modernizr.load([
{
load: [
'../includes/css/foo.css',
'../includes/js/foo.js',
'../includes/js/bar.js'
],
complete: function() {
initBar();
}
}
]);
Code 2 (functional)
Modernizr.load([
{
load: [
'../includes/css/foo.css',
'../includes/js/foo.js',
'../includes/js/bar.js'
],
complete: function() {
window.initBar();
}
}
]);
bar.js
var initBar = function() {
// code here
};
It works fine in other browsers. I've tried moving the blocks to the head section as well as beneath the page. I've also tried wrapping the contents of the callback in $(document).ready()
, but none have worked with code 1.
The error I am getting specifically is:
SCRIPT5009: « initBar » est indéfini
It is almost as if the callback is executed before the resources are finished loading, but if that was the case then why does code sample 2 work?
Also I will note that on refresh the page loads fine (most likely due to the fact that the resources are cached), but it also loads fine after clearing the cache. I have to restart my browser session after clearing the cache to reproduce the problem.
UPDATE: This problem extends to more than just functions. Any global variable defined in a JS file that is loaded doesn't seem to be accessible directly. It also occurs if I load the CSS at the top of the page rather than with the other resources asynchronously. In fact I'm also noticing this problem with some jQuery plugins that are loaded in this manner.
UPDATE 2:
Here is the console.log()
output as per debugging instructions below. I've changed bar to be an object instead of a function for the sake of illustrating this.
Internet Explorer:
HTML1300: Une navigation s’est produite.
Fichier : test18.php
before .load() called
before bar accessed
typeof bar = undefined
typeof window.bar = undefined
SCRIPT5009: « bar » est indéfini
Fichier : test18.js, ligne : 14, colonne : 13
before bar defined
So it appears that the complete
function executes before bar
is defined. I find it strange that window.bar
is also undefined yet works...
Firefox
[02:10:46,448] "before .load() called"
[02:10:47,184] "before bar defined"
[02:10:47,184] "before bar accessed"
[02:10:47,184] "typeof bar = object"
[02:10:47,184] "typeof window.bar = object"
Chrome
before .load() called
before bar defined
before bar accessed
typeof bar = object
typeof window.bar = object
Both Firefox and Chrome appear to be loading and executing the resources in the correct order.