Last active
March 23, 2017 11:27
-
-
Save evolutionxbox/e06e2ad5b8c2ea52d9252fb0fb501d40 to your computer and use it in GitHub Desktop.
Load those scripts all async like!
This file contains hidden or 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
// 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