Last Updated: February 25, 2016
·
1.787K
· herschel666

PNG-Fallback for SVG-Images

Inspired by this snippet: http://codepad.co/s/02aac9, I wrote an alternate version that doesn't depend on jQuery for replacing all SVG-suffixes with the PNG-suffix on inline-images in the markup of a site.

One just has to provide the referenced fallback-images.

var rSvg = /\.(svg)$/;

if ( !Modernizr.svg ) {
  for ( var i = 0, img; img = document.images[i]; i += 1 ) {
    if ( !rSvg.test(img.src) ) continue;
    img.src = img.src.replace(rSvg, '.png');
  }
}

The only dependency is Modernizr.

(https://gist.github.com/herschel666/5024092)

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside