Skip to content

Instantly share code, notes, and snippets.

@pseudosavant
Created February 27, 2017 22:14
Show Gist options
  • Save pseudosavant/81fd60585f23f6d257af9378febc5ea8 to your computer and use it in GitHub Desktop.
Save pseudosavant/81fd60585f23f6d257af9378febc5ea8 to your computer and use it in GitHub Desktop.
Basic script loader in JavaScript with optional callback on `load`
// Loads an external js file and optionally calls a callback on completion
function loadScript(url, callback) {
var script = document.createElement('script');
document.body.appendChild(script);
if (typeof callback === 'function') {
script.addEventListener('load', callback, false);
}
script.src = url;
return script;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment