Created
May 21, 2018 14:17
-
-
Save panphora/f5f3138113a2d1ad64ff57df4d066f82 to your computer and use it in GitHub Desktop.
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
function getScript(source, callback) { | |
var scriptElement = document.createElement('script'); | |
var priorScriptElement = document.getElementsByTagName('script')[0]; | |
scriptElement.async = 1; | |
scriptElement.onload = scriptElement.onreadystatechange = function( _, isAbort ) { | |
if (isAbort || !scriptElement.readyState || /loaded|complete/.test(scriptElement.readyState) ) { | |
scriptElement.onload = scriptElement.onreadystatechange = null; | |
scriptElement = undefined; | |
if (!isAbort) { | |
if (callback) { | |
callback(); | |
} | |
} | |
} | |
}; | |
scriptElement.src = source; | |
priorScriptElement.parentNode.insertBefore(scriptElement, priorScriptElement); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment