Skip to content

Instantly share code, notes, and snippets.

@thomasmb
Created October 24, 2013 07:09
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thomasmb/7132605 to your computer and use it in GitHub Desktop.
Save thomasmb/7132605 to your computer and use it in GitHub Desktop.
Loading gist embeds dynamically
// … 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 )
});
});
}
@xdumaine
Copy link

Thanks for your post on this - I couldn't find any docs or reference on using the json method.

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