Last active
August 29, 2015 14:22
-
-
Save geerteltink/f0c2d35ba43bc3490ff0 to your computer and use it in GitHub Desktop.
Loading gists async without jquery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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]); | |
| } | |
| } | |
| })(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on https://gist.github.com/JeanSebTr/4138162.
Usage:
<gist data-id="f0c2d35ba43bc3490ff0" data-file="embed-gist.js"></gist>