Skip to content

Instantly share code, notes, and snippets.

@kfox
Created November 25, 2014 16:30
Show Gist options
  • Save kfox/f10abf8da83e0bb70e12 to your computer and use it in GitHub Desktop.
Save kfox/f10abf8da83e0bb70e12 to your computer and use it in GitHub Desktop.
Replace SVG images loaded via img tags with inlined svg elements
inline_svg_images = (svg) ->
svg_selector = if svg? and $(svg)? then svg else 'img.svg'
$(svg_selector).each ->
$img = $(@)
$.get $img.attr('src'), (data) ->
$svg = $(data).find 'svg'
for attribute in [ 'id', 'class', 'height', 'width' ]
if $img.attr(attribute)?
$svg = $svg.attr attribute, $img.attr attribute
$svg = $svg.removeAttr 'xmlns:a'
$img.replaceWith $svg
, 'xml'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment