Last active
March 26, 2023 09:14
-
-
Save furzeface/7697041 to your computer and use it in GitHub Desktop.
The snippets I found online to replace svg with png for legacy browsers using Modernizr were all a little flaky, so here's what I wrote.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use Modernizr to check | |
if (!Modernizr.svg) { | |
// Using jQuery | |
$('img').each(function () { // Could also be more specific if you know where the svg's will be e.g. $('#work-content img') | |
var $img = this, // Cache current image DOM element in a variable | |
src = $img.src.split('.'), // Split the source | |
extension = src[src.length - 1]; // Make sure it's the last part of the split string | |
if (extension === 'svg') { | |
src[src.length - 1] = 'png'; // If it's svg, do all the things | |
$img.src = src.join('.'); // Join it back up and change the img source | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment