Snippet of deferred loader
(function(){
function deferred(success){
if (typeof window.onload != 'function') {
window.onload = success;
} else {
var old = window.onload ;
window.onload = function() {
old();
success();
}
}
}
function getScript(url,success){
var script=document.createElement('script');
script.src=url;
var head=document.getElementsByTagName('head')[0],done=false;
script.onload=script.onreadystatechange = function(){
if ( !done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') ) {
done=true;
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
function getScripts(urls, success){
var total = 0;
function trySuccess(){
total++;
if(total == urls.length)success();
}
for (var i = 0; i < urls.length; i++) {
getScript(urls[i],trySuccess);
}
}
var DeferredLoader = window.DeferredLoader = {};
DeferredLoader.getScript = getScript;
DeferredLoader.getScripts = getScripts;
DeferredLoader.deferred= deferred;
})();
How to use loading, in that example we load jQuery, then when it's loaded we load kissmetrics libraries and only then utils.
var urls = {
jquery: "/javascript/jquery-1.9.1.min.js",
dependent: ["/javascript/util-2013-04-10m.js"],
kissmetrics:['//i.kissmetrics.com/i.js','//doug1izaerwt3.cloudfront.net/' + window._kmk + '.1.js' ]
};
DeferredLoader.deferred(function () {
DeferredLoader.getScript(urls.jquery, function () {
DeferredLoader.getScripts(urls.kissmetrics, function () {
DeferredLoader.getScripts(urls.dependent, function(){})
})
})
})
1 comment :
Nice one. Letting users see the page before the behavior is loaded at least speeds the perception. Now that one as tickbox in XPages would be perfect
Post a Comment