Skip to content

Instantly share code, notes, and snippets.

@noscript
Forked from JeanSebTr/gists.js
Last active October 15, 2016 22:35
Show Gist options
  • Save noscript/036a0bf6c08a199c7520954968ff2d65 to your computer and use it in GitHub Desktop.
Save noscript/036a0bf6c08a199c7520954968ff2d65 to your computer and use it in GitHub Desktop.
Async load of Github's gists without jquery in 31 lines of code
(function(){
var gists = document.getElementsByTagName('gist');
function embed(id, file, i, tag) {
window['embed_gist_' + i] = function(gist) {
var div = document.createElement('div');
div.innerHTML = gist.div;
tag.parentNode.replaceChild(div.firstChild, tag);
var css = document.createElement('link');
css.rel = 'stylesheet';
css.href = gist.stylesheet;
document.head.appendChild(css);
};
var url = 'https://gist.github.com/' + id + '.json?callback=embed_gist_' + i;
if (file) {
url += '&file=' + file;
}
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
document.head.appendChild(script);
}
for (var i = 0; i < gists.length; i++) {
var id = gists[i].getAttribute('data-id');
var file = gists[i].getAttribute('data-file');
embed(id, file, i, gists[i]);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment