I'm looking for 开发者_开发问答the best solution to dynamically change some images extension (from .svg to .png) only if the browser is IE.
I don't know what's the best solution :
- parse the html code with PHP
use jQuery with something like
$("img.svg2png").attr("src", ...);
dealing with htaccess and rewrite rules
- other solution ?
Thanks !
You're not revealing many details about what you're doing, but the mod_rewrite solution (catching the USER_AGENT
variable and checking whether it is IE, and redirecting internally to the matching .png
file) sounds the most elegant to me, because it works sans JavaScript, and you can keep the file extension. The .svg
extension should be meaningless as long as the right Content-Type
header is sent.
you could do this:
var imageArray = $('img.svg2png').attr('src').split('.');
var newImage = imageArray[0]+'.png';
$("img.svg2png").attr("src", newImage);
keep in mind that this is assuming that you only have 1 period in the full src of the file (that being extention period)
to break it down, what .split('.') does is create an array of strings where the period is a delimiter. so
imageArray[0] = 'imagesDir/imageName'
and
imageArray[1] = 'svg'
so what you are doing is reconstructing the src with the first part of the image and a new extention.
hope this helps!
精彩评论