Skip to content

Instantly share code, notes, and snippets.

@morganney
Last active August 29, 2015 13:55
Show Gist options
  • Select an option

  • Save morganney/8757562 to your computer and use it in GitHub Desktop.

Select an option

Save morganney/8757562 to your computer and use it in GitHub Desktop.
Function to Insert Gists Dynamically.
/*
* Embeds Gists into specified DOM elements after fetching them.
* Assumes Gists are to be embedded into elements with an HTML5 data-* attribute,
* with a value equal to a public Gist id.
*
* @param {String:dataAttrName} The data attribute name value.
* For example <code data-gistId='12345'></code> means the dataAttrName === "gistId".
*/
function embedGists(dataAttrName) {
var dataAttr = "data-" + dataAttrName;
$("[" + dataAttr + "]").each(function(idx, el) {
fetch($(el).attr(dataAttr)).then(function(gist) {
addGistCss(gist.stylesheet); // Utility function to add stylesheet from GitHub.
$(el).html(gist.div);
}).catch(error) {
$(el).html("Something went wrong: " + error.message);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment