Saturday, August 23. 2008Javascript To Change Image On Link Click
I recently decided to add the ability for guests to request a new captcha image (or refresh the captcha image) in my guestbook script for when it was to hard for visitors to read. As usual trying to find some instructions to do this proved useless so I had to figure it out myself. I thought I would explain how it's done here for anyone else wanting to do this and believe me it's very very simple. Ok first thing we need to do is give a name to the image we are going to by replacing/changing so we add a name. For the guestbook script I added added name="lazCaptcha" to the image tag. eg: CODE: <img src="http://domain.com/path/to/image/image.jpg" name="NAMEFORIMAGE" alt="" /> Next we need to create the link that the viewer clicks on to replace/change the image and for that I used this bit of code: CODE: <a href="#" onclick="reloadImage('NAMEFORIMAGE'); return false;">request another image</a> Notice how I put the name of the image into the Javascript? Thats so we can use this code multiple times on a single page. Next we need the Javascript and it is very simple. We just create a random number to add to the image url to prevent it showing the cached image and then tell the browser to change the image. I'll show two examples. The first i |