Created
October 24, 2013 07:09
-
-
Save thomasmb/7132605 to your computer and use it in GitHub Desktop.
Loading gist embeds dynamically
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
// … 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 ) | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your post on this - I couldn't find any docs or reference on using the json method.