Last active
August 29, 2015 14:02
-
-
Save khwas/3bbeb67d944656dbd52c to your computer and use it in GitHub Desktop.
Json style of embeeding gist from github using plain HTML and scripts
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
// 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