Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jnrbsn
Created December 12, 2012 14:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jnrbsn/4268258 to your computer and use it in GitHub Desktop.
Save jnrbsn/4268258 to your computer and use it in GitHub Desktop.
Loads a JavaScript file asynchronously with a callback, like jQuery's `$.getScript()` except without jQuery.
function j(u, c) {
var h = document.getElementsByTagName('head')[0], s = document.createElement('script');
s.async = true; s.src = u;
s.onload = s.onreadystatechange = function () {
if (!s.readyState || /loaded|complete/.test(s.readyState)) {
s.onload = s.onreadystatechange = null; if (h && s.parentNode) { h.removeChild(s) } s = undefined;
if (c) { c() }
}
};
h.insertBefore(s, h.firstChild);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment