View Full Version : Static Gif To OnClick Gif With Link

01-16-2012, 04:19 AM
I'm looking for a simple code to perform this function I'm 3/4 of the way there but need some help.

I have a static GIF that is swapped for an animated gif after an onclick event. I want the animated gif to have a clickable URL that is activated once the user clicks on the static GIF.

Here is the current code I have to show the animated gif from the onclick event, how do I add a web site URL to make the animated.gif a clickable image link?

<img src="static.gif" width="620" height="549" onclick='javascript:(this.src=="static.gif"?this.src="animated.gif":"")' /></a>

01-16-2012, 05:17 AM
If you want a solution, why not provide the static and animated GIF files to test with?
Also, what the the URL you wish to go to when the animated GIF is visible?

Old Pedant
01-16-2012, 06:21 AM
Why not do this?

<img src="static.gif" width="620" height="549"
onclick="if ( this.src.indexOf('static') >= 0 ) { this.src='animated.gif'; }
else { location.href='somePage.html'; } " />

You should NOT test for this.src=='static.gif' because some browsers will replace your src with the full URL to the image (e.g., "http://www.yoursite.com/static.gif"). By just looking to see if "static" is part of the image.src, you avoid problems.