Skip to content

Instantly share code, notes, and snippets.

@pixiebox
Created October 23, 2016 10:19
Show Gist options
  • Save pixiebox/b5b5b6f688494804b087745883c4b24b to your computer and use it in GitHub Desktop.
Save pixiebox/b5b5b6f688494804b087745883c4b24b to your computer and use it in GitHub Desktop.
dynamic script with callback
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