Skip to content

Instantly share code, notes, and snippets.

@nomadrat
Created January 24, 2016 19:45
Show Gist options
  • Save nomadrat/7208d3955e19cd5e6beb to your computer and use it in GitHub Desktop.
Save nomadrat/7208d3955e19cd5e6beb to your computer and use it in GitHub Desktop.
function loadScript(src, fn) {
var script, done, head;
script = document.createElement('script');
script.src = src;
script.async = true;
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete')) {
done = true;
script.onload = script.onreadystatechange = null;
if (fn) {
fn();
}
if (script && script.parentNode) {
script.parentNode.removeChild(script);
}
}
};
if (!head) {
head = document.getElementsByTagName('head')[0];
}
head.appendChild(script);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment