Skip to content

Instantly share code, notes, and snippets.

@evolutionxbox
Last active March 23, 2017 11:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save evolutionxbox/e06e2ad5b8c2ea52d9252fb0fb501d40 to your computer and use it in GitHub Desktop.
Load those scripts all async like!
// TODO: convert into promise =)
// Load Script
function loadScript(url, callback) {
if (!url || url === '') return;
var scriptElement = document.createElement('script');
scriptElement.src = url;
if(typeof callback === 'function') {
scriptElement.onload = callback;
}
document.body.insertAdjacentElement('beforeend', scriptElement);
}
// Load Script Files
/*
* Expects array of objects in the format:
* {
* url: 'path/to/script.js',
* callback: function () {}
* }
*
* The callback is optional.
*
*/
function loadScripts(scripts, complete) {
if (scripts.length > 0) {
scripts.forEach(function(script) {
loadScript(script.url, script.callback);
});
if (typeof complete === 'function') {
complete();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment