Skip to content

Instantly share code, notes, and snippets.

@khwas
Last active August 29, 2015 14:02
Show Gist options
  • Save khwas/3bbeb67d944656dbd52c to your computer and use it in GitHub Desktop.
Save khwas/3bbeb67d944656dbd52c to your computer and use it in GitHub Desktop.
Json style of embeeding gist from github using plain HTML and scripts
// Original article https://bensmann.no/dynamically-embedding-gists/
// … find all gist scripts inside the ajax container
var $gists = $ajax_container.find('script[src^="https://gist.github.com/"]');
// if gist embeds are found
if( $gists.length ){
// update each gist
$gists.each(function(){
// we need to store $this for the callback
var $this = $(this);
// load gist as json instead with a jsonp request
$.getJSON( $this.attr('src') + 'on?callback=?', function( data ) {
// replace script with gist html
$this.replaceWith( $( data.div ) );
// load the stylesheet, but only once…
add_stylesheet_once( 'https://gist.github.com/' + data.stylesheet )
});
});
function add_stylesheet_once( url ){
$head = $('head');
if( $head.find('link[rel="stylesheet"][href="'+url+'"]').length < 1 )
$head.append('<link rel="stylesheet" href="'+ url +'" type="text/css" />');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment