I am trying to write an ultra simple solution to load a bunch of JS files asynchronously. I have the following script below so far. However the callback is sometimes called when the scripts aren't actually loaded which causes a variable not found error. If I refresh the page sometimes it just works because I guess the files are coming straight from the cache and thus are there quicker than the callback is called, it's very strange?
var Loader = function () {
}
Loader.prototype = {
require: function (scripts, callback) {
this.loadCount = 0;
this.totalRequired = scripts.length;
this.callback = callback;
for (var i = 0; i < scripts.length; i++) {
this.writeScript(scripts[i]);
}
},
loaded: function (evt) {
this.loadCount++;
if (this.loadCount == this.totalRequired && typeof this.callback == 'function') this.callback.call();
},
writeScript: function (src) {
var self = this;
var s = document.createElement('script');
s.type = "text/javascript";
s.async = true;
s.src = src;
s.addEventListener('load', function (e) { self.loaded(e); }, false);
var head = document.getElementsByTagName('head')[0];
head.appendChild(s);
}
}
Is there anyway to test that a JS file is completely loaded, without putting something in the actual JS file itself, because I would like to use the same pattern to load libraries out of my control (GMaps etc).
Invoking code, just before the tag.
var l = new Loader();
l.require([
"ext2.js",
"ext1.js"],
function() {
var config = new MSW.Config();
Refraction.Application().run(MSW.ViewMapper, config);
console.log('All Scripts Loaded');
});
Thanks for any help.
There is nothing wrong with your code from what I can tell, this is just a bug in Chrome (it does it with window.onload also.)
I'd add it to the function that is triggered in the "load" function. If the variable exists, execute the JS code, but if it doesn't, use a setTimeout to check again in 500ms or so.
Just in case you find this useful, I've created an async utility library that would let you write the above code as:
If you're doing lots of asynchronous calls it has some quite powerful features :)
http://caolanmcmahon.com/async.html
What about jQuery....
Above code will load the "abc.js" script file asynchronously....
jcors-loader.js not working in Internet Explorer...
Index.html
alertme.js
alert("Loaded");
This works fine in chrome and firefox it displays "TEST OK" and popup...But no message or alert in IE(7,8,9)...Any help will be appreciated.
Aku menggabungkan scrpit callback dengan tooltip, didalam template blogger. kemudian didalamnya ditambahkan script callbak menggunakan javascipt
Script callback with tooltip
And you can see the demo at harga lantai kayu
I recommend you use a tiny loader javascript like JcorsLoader (only 647B with Gzip)
Load multiples js in parallel and execute in order without blocking DOMReady or onload.
https://github.com/pablomoretti/jcors-loader