-
-
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
This file contains 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['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