Skip to content

Instantly share code, notes, and snippets.

@nobuti
Forked from judofyr/load.js
Created September 5, 2012 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobuti/3633513 to your computer and use it in GitHub Desktop.
Save nobuti/3633513 to your computer and use it in GitHub Desktop.
function loadScript(path, fn) {
var el = document.createElement('script'),
loaded = 0,
onreadystatechange = 'onreadystatechange',
readyState = 'readyState';
el.onload = el.onerror = el[onreadystatechange] = function () {
if (loaded || (el[readyState] && !(/^c|loade/.test(el[readyState])))) return;
el.onload = el.onerror = el[onreadystatechange] = null;
loaded = 1;
fn();
};
el.async = 1;
el.src = path;
document.getElementsByTagName('head')[0].appendChild(el);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment