Created
October 23, 2016 10:19
-
-
Save pixiebox/b5b5b6f688494804b087745883c4b24b to your computer and use it in GitHub Desktop.
dynamic script with 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
function dynamicAsset(script_name, cb) { | |
var body = document.body, | |
script = document.createElement('script'), | |
done = false; | |
script.type = 'text/javascript'; | |
script.src = lang.templateUrl + '/dist/scripts/' + script_name; | |
script.onload = script.onreadystatechange = function(){ | |
if ( !done && (!this.readyState || | |
this.readyState === "loaded" || this.readyState === "complete") ) { | |
done = true; | |
if (!!(cb && cb.constructor && cb.call && cb.apply)) { | |
cb(); | |
} | |
} | |
}; | |
body.appendChild(script); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment