Thursday, January 28, 2010

Reloading Captcha Images in JavaScript

The problem: when the form input is rejected, we want to load a new captcha. However, the browser might not understand that we want to load a new image in place of the one in the cache. Why? Because the image looks like this:
<img src="captcha.php" id="img_captcha">
and the JavaScript to reload the image looks like this:
document.getElementById('img_captcha').src = 'captcha.php';
Thus, from the browser's perspective, the image source should stay the same.

The solution: add a '#' to the end of the url:
document.getElementById('img_captcha').src = 'captcha.php';

No comments:

Post a Comment