Skip to content

Instantly share code, notes, and snippets.

@geerteltink
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save geerteltink/f0c2d35ba43bc3490ff0 to your computer and use it in GitHub Desktop.

Select an option

Save geerteltink/f0c2d35ba43bc3490ff0 to your computer and use it in GitHub Desktop.
Loading gists async without jquery
(function(){
var gists = document.getElementsByTagName('gist');
function embed(id, file, i, tag) {
window['gist_embed_'+i] = function(gist) {
console.log(gist);
if (!document.getElementById('gist_css_'+id)) {
var css = document.createElement('link');
css.id = 'gist_css_'+id;
css.rel = 'stylesheet';
css.href = gist.stylesheet;
document.head.appendChild(css);
}
var tmp = document.createElement('div');
tmp.innerHTML = gist.div;
tag.parentNode.replaceChild(tmp.firstChild, tag);
};
var url = 'https://gist.github.com/'+id+'.json?callback=gist_embed_'+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');
if(id) {
gists[i].innerHTML = '<a href="https://gist.github.com/'+id+'">Loading gist...</a>';
embed(id, file, i, gists[i]);
}
}
})();
@geerteltink
Copy link
Copy Markdown
Author

Based on https://gist.github.com/JeanSebTr/4138162.

Usage: <gist data-id="f0c2d35ba43bc3490ff0" data-file="embed-gist.js"></gist>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment