Github Gists Logo

I have been building a small project boilerplate for when I am starting new projects. I wrote this small snippet based on this article.

The only changes I made were wrapping it in a closure and combining all the vars to make it smaller. Of course your minifier would do this anyway unless you are using it inline after including Modernizr.

Here is the gist.

Or you can copy the current version from right here.

if (!Modernizr.svg) {
  // wrap this in a closure to not expose any conflicts
  (function() {
    // grab all images. getElementsByTagName works with IE5.5 and up
    var imgs = document.getElementsByTagName('img'),endsWithDotSvg = /.*\.svg$/,i = 0,l = imgs.length;
    // quick for loop
    for(; i < l; ++i) {
      if(imgs[i].src.match(endsWithDotSvg)) {
        // replace the png suffix with the svg one
        imgs[i].src = imgs[i].src.slice(0, -3) + 'png';
      }
    }
  })();
}