Created
April 4, 2014 13:13
-
-
Save hekt/9974458 to your computer and use it in GitHub Desktop.
JavaScript 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 jsLoader() { | |
"use strict"; | |
var scripts = [].slice.call(arguments); | |
var reserves = []; | |
var thisScript = document.scripts[0] || document.querySelector("script"); | |
var head = thisScript.parentNode; | |
if ("async" in thisScript) | |
parallel(); | |
else if (thisScript.readyState) | |
parallelForIE(); | |
else | |
sequential(); | |
function parallel() { | |
scripts.forEach(function (script) { | |
var elem = document.createElement("script"); | |
elem.async = false; | |
elem.src = script; | |
head.insertBefore(elem, thisScript); | |
}); | |
} | |
function parallelForIE() { | |
scripts.forEach(function (script) { | |
var elem = document.createElement("script"); | |
reserves.push(elem); | |
elem.onreadystatechange = stateChange; | |
elem.src = script; | |
}); | |
} | |
function sequential() { | |
var script = scripts.shift(); | |
var elem; | |
if (script) { | |
elem = document.createElement("script"); | |
elem.src = script; | |
elem.onload = sequential; | |
head.insertBefore(elem, thisScript); | |
} | |
} | |
function stateChange() { | |
var elem; | |
while (reserves[0] && reserves[0].readyState === "loaded") { | |
elem = reserves.shift(); | |
elem.onreadystatechange = null; | |
head.insertBefore(elem, thisScript); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment