Last active
September 11, 2018 22:08
-
-
Save alexisdiel/34bf057a4f521b0ba529587d8eac3661 to your computer and use it in GitHub Desktop.
dynamic loading javascript files with javascript without jQuery (getScript)
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
function getScript(source, callback) { | |
if (Array.isArray(source)) | |
getScript(source[0], (source.length > 1) ? () => getScript(source.slice(1), callback) : callback); | |
else if (!document.querySelector(`script[src="${source}"]`)) { | |
var el = document.createElement('script'); | |
el.onload = callback; | |
el.src = source; | |
document.head.appendChild(el); | |
} else { | |
callback(); | |
} | |
} | |
/** Uses **/ | |
getScript([ | |
"https://cdnjs.cloudflare.com/ajax/libs/anylibrary/1.1.0/main.js", | |
"https://cdnjs.cloudflare.com/ajax/libs/anylibrary/1.1.0/main-addon.js", | |
"https://cdnjs.cloudflare.com/ajax/libs/anylibrary/1.1.0/main-addon-lang.js" | |
], () => { alert("all loaded"); }); | |
/*OR*/ | |
getScript("https://cdnjs.cloudflare.com/ajax/libs/anylibrary/1.1.0/main.js", () => { alert("main loaded"); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment