Skip to content

Instantly share code, notes, and snippets.

@akhawaja
Created October 23, 2016 20:23
Show Gist options
  • Save akhawaja/0d6cda05391b00659402711eef96a618 to your computer and use it in GitHub Desktop.
Save akhawaja/0d6cda05391b00659402711eef96a618 to your computer and use it in GitHub Desktop.
Load JavaScripts from a URL.
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