Skip to content

Instantly share code, notes, and snippets.

@crishushu
Created June 5, 2014 09:21
Show Gist options
  • Save crishushu/a7ea483d118d35b68459 to your computer and use it in GitHub Desktop.
Save crishushu/a7ea483d118d35b68459 to your computer and use it in GitHub Desktop.
JavaScript: Async js/css file loader
function load (arr, suc, err) {
var libs = [];
for (var i = 0, len = arr.length; i < len; i++) {
var el = arr[i];
// var domElem = null;
var extension = el.split(".")[el.split(".").length - 1];
switch (extension) {
case "css":
(function() {
$("<link/>", {
rel: "stylesheet",
type: "text/css",
href: el
}).appendTo("head");
})();
break;
case "js":
(function() {
if (!suc) {
$("<script/>", {
type: "text/javascript",
src: el
}).appendTo("head");
} else {
libs.push(el);
}
})();
break;
default:
console.warn("file not found: ", el);
}
}
// synched version with callback
if (libs.length > 0) {
$.when.apply(this, libs.map(function(src) {
return $.getScript(src);
})).done(suc).fail(err);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment