If I work with <img> elements (as opposed to CSS background images), I use a convenient workaround, a combination of Modernizr (a JavaScript library that detects the availability of certain functions , such as .svg support, in browsers) and several jQuery lines:
$(".no-svg img").each(function() { var $this = $(this); if ($this.attr("src").indexOf(".svg") > -1) { var isSvg = $this.attr("src").toString(); var isPng = isSvg.replace(".svg", ".png"); $this.attr("src", isPng); } });
1) I create a .png version of each .svg file. 2) Modernizr provides the html element with the .no-svg class if it detects that there is no .svg support. 3) jQuery swaps file extensions. .indexOf(".svg") checks if the string ".svg" is contained within the src value, returning -1 if it does not find it and a positive integer if that happens. If it finds ".svg" , the entire src string is pulled into isSvg , and .replace() changes .svg to .png and saves the result as isPng , which is then set to src .
Danny Oct 02 '14 at 2:14 on 2014-10-02 02:14
source share