Skip to content

Instantly share code, notes, and snippets.

@sampotts
Last active August 23, 2017 05:00
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 sampotts/6ed29257af53dab18d45a75f3a632087 to your computer and use it in GitHub Desktop.
Save sampotts/6ed29257af53dab18d45a75f3a632087 to your computer and use it in GitHub Desktop.
// Async load a script with a callback
(function () {
function async(u, c, a) {
var d = document;
var t = 'script';
var o = d.createElement(t);
var s = d.getElementsByTagName(t)[0];
o.src = u;
if (typeof a === 'object') {
for (var attr in a) {
s.setAttribute(attr, a[attr]);
}
}
if (c) {
o.addEventListener('load', function (e) {
c(null, e);
}, false);
}
s.parentNode.insertBefore(o, s);
}
async('https://path.to/your/script.js', function () {
// This will get called on load
}, {
// Any attributes that need adding to the script tag as key/value pairs
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment