Skip to content

Instantly share code, notes, and snippets.

@stuwilli
Last active August 29, 2015 14:10
Show Gist options
  • Save stuwilli/307b40d3e9605525e165 to your computer and use it in GitHub Desktop.
Save stuwilli/307b40d3e9605525e165 to your computer and use it in GitHub Desktop.
Load external JS script
/**
* Import/include an additional script in the document head
* @param {string} src the source file
* @param {function} callback the callback function
*/
function importScript(src, callback) {
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', src);
if (typeof callback === 'function') {
script.onload = callback;
script.onreadystatechange = function() {
if (this.readyState == 'complete') {
callback();
}
};
}
document.head.appendChild(script);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment