Skip to content

Instantly share code, notes, and snippets.

@0xDaksh
Forked from jnrbsn/gist:4268258
Created November 22, 2017 09:19
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 0xDaksh/384af6911e702397ca7315ac08f91672 to your computer and use it in GitHub Desktop.
Save 0xDaksh/384af6911e702397ca7315ac08f91672 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