Created
June 5, 2014 09:21
-
-
Save crishushu/a7ea483d118d35b68459 to your computer and use it in GitHub Desktop.
JavaScript: Async js/css file loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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