Created
October 23, 2016 20:23
-
-
Save akhawaja/0d6cda05391b00659402711eef96a618 to your computer and use it in GitHub Desktop.
Load JavaScripts from a URL.
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
if (!window.loadScripts) { | |
/** | |
* Load scripts given an array of URLs. | |
* @author Amir Khawaja <[email protected]> | |
* @license MIT | |
* @param {array} scripts - An array of URLs of scripts. | |
* @param {function} done - The function to call once the scripts are loaded. | |
*/ | |
window.loadScripts = function (scripts, done) { | |
var loader = function (src, handler) { | |
var script = document.createElement('script'); | |
script.src = src; | |
script.onload = script.onreadystatechange = function () { | |
script.onreadystatechange = script.onload = null; | |
handler(); | |
}; | |
var head = document.getElementsByTagName('head')[0]; | |
(head || document.body).appendChild(script); | |
}; | |
(function run() { | |
if (scripts.length != 0) { | |
loader(scripts.shift(), run); | |
} else { | |
done && done(); | |
} | |
})(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment