Skip to content

Instantly share code, notes, and snippets.

@furzeface
Last active March 26, 2023 09:14
Show Gist options
  • Save furzeface/7697041 to your computer and use it in GitHub Desktop.
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.
// 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