I am using simple captcha in my JSP. Everything is OK. I want to provide a refresh button alongside of captcha to allow user to change the captcha. Captcha changes only on refreshing the plete page but I don't want to reload whole page for it.
I need your suggestions on how I can implement this like using AJAX or JQuery to reload only the captcha, not whole page.
I am using simple captcha in my JSP. Everything is OK. I want to provide a refresh button alongside of captcha to allow user to change the captcha. Captcha changes only on refreshing the plete page but I don't want to reload whole page for it.
I need your suggestions on how I can implement this like using AJAX or JQuery to reload only the captcha, not whole page.
Share Improve this question edited Apr 14, 2012 at 7:48 Andrew Thompson 169k41 gold badges222 silver badges436 bronze badges asked Apr 14, 2012 at 7:37 SandySandy 14.1k22 gold badges79 silver badges111 bronze badges 03 Answers
Reset to default 3<script type="text/javascript">
function reloadCaptcha(){
var d = new Date();
$("#captcha_image").attr("src", "/captcha_generator.jsp?"+d.getTime());
}
</script>
...
<img id="captcha_image" src="/captcha_generator.jsp" alt="captcha image" width="200" height="50"/>
<img src="reload.jpg" onclick="reloadCaptcha()" alt="reload"width="40" height="40"/>
I don't remember how SimpleCaptcha works, but usually, you should simply change the 'src' attribute of you captcha <img>
. Something like this on the onClick
of your refresh button:
var img = document.getElementById('captcha_id');// captcha_id is the id attribute of your caprtcha img
img.src = 'Some new captcha url';
<script type="text/javascript">
function reloadCaptcha(){
var d = new Date();
$("#captcha_image").attr("src", "captcha.php?"+d.getTime());
} </script>
<img id="captcha_image" src="captcha.php" alt="captcha image" width="90" height="33"/> <img src="images/refresh.png" alt="reload" width="22" height="22" border="0" onclick="reloadCaptcha()" />