tags:

views:

22

answers:

2

Hi,

Is it possible to replace all PNGs across the board with GIFs using jquery? How would I do this?

Thanks

+2  A: 

Do you have the images as gifs already?

If you do, it will be as easy as doing something like:

$('img').each(function() {
     var src = $(this).attr("src").replace(".png", ".gif");
     $(this).attr("src", src);
});

This will go through all the img tags, and replace the path with it's own path, but with the gif extension instead of png

Let me know if that helps you.

Marcos Placona
What about `<img src="groupngrope.jpg"/>`? You should at least add the dot (and if you're a perfectionist, a `$|\?` and convert it to an regexp).
Boldewyn
Thought about the regexp (my preferred choice normally), but wanted to give him a simple example, so he could understand. the groupngroup is a valid thing though. Have already updated my code to use a .+1 for you :-)
Marcos Placona
Thanks for your response, worked perfectly :)
Probocop
+1, that's the way to go in 99.99% of all cases.
Boldewyn
A: 

Are all PNGs referenced as '.png'? If not, it isn't possible with JS alone (see Mozilla's doc on the Image object: No property re. the mime type).

Else try mplacona's solution.

Edit: To give an example: This URL

http://www.w3.org/Icons/valid-html40

leads to an image, but you cannot guess, which format it is. You can explicitly request

http://www.w3.org/Icons/valid-html40.gif

or

http://www.w3.org/Icons/valid-html40.png

but, without extension, the server decides, what it sends, and the client (JavaScript, that is) has no means to decide what will come.

Boldewyn