Skip to content

Instantly share code, notes, and snippets.

@Tickthokk
Created March 27, 2013 18:16
Show Gist options
  • Save Tickthokk/5256705 to your computer and use it in GitHub Desktop.
Save Tickthokk/5256705 to your computer and use it in GitHub Desktop.
// Special thanks to: http://stackoverflow.com/a/11978996/286467
$('img.svg').each(function() {
var img = $(this);
if (img.hasClass('no-replace'))
return;
$.ajax({
url: img.attr('src'),
type: 'GET',
dataType: "xml",
success:function(data) {
// Parse the SVG portion out of the request
var svg = $(data).find('svg');
// Add the ID in
if (typeof(img.attr('id')) !== 'undefined')
svg = svg.attr('id', img.attr('id'));
// Add the class in
if (typeof(img.attr('class')) !== 'undefined')
svg = svg.attr('class', img.attr('class') + ' replaced-svg');
// Remove any invalid XML tags as per http://validator.w3.org
svg = svg.removeAttr('xmlns:a');
// Prevent "0" from being interpreted as a real color
if (img.data('color') === 0)
img.data('color', '');
if (img.data('bg') === 0)
img.data('bg', '');
// Colorize
svg = osa.svg_colorize(svg, img.data('color'), img.data('bg'));
// Replace image with new SVG
img.replaceWith(svg);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment