-
-
Save andrijac/b71f9ffb62e74f98473d107864eea138 to your computer and use it in GitHub Desktop.
Dynamically load JavaScript files with callback when finished
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
atm.loadScript = (function () { | |
let loadedScripts = []; | |
return function (src, callback) { | |
if (~loadedScripts.indexOf(src) && callback) { | |
callback(); | |
} | |
$.ajax({ | |
url: src, | |
dataType: 'script', | |
success: innerCallback, | |
async: true, | |
error: function () { | |
console.log(error); | |
} | |
}); | |
function innerCallback(script) { | |
//eval(script); | |
loadedScripts.push(src); | |
callback(); | |
} | |
}; | |
})(); |
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
loadScript = (function () { | |
let loadedScripts = []; | |
return function (src, callback) { | |
if (~loadedScripts.indexOf(src) && callback) { | |
callback(); | |
} | |
let script = document.createElement('script'), | |
loaded; | |
script.setAttribute('src', src); | |
if (callback) { | |
script.onreadystatechange = script.onload = function () { | |
if (!loaded) { | |
callback(); | |
} | |
loaded = true; | |
loadedScripts.push(src); | |
}; | |
} | |
document.getElementsByTagName('head')[0].appendChild(script); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment