Reputation: 29
How can I get the image source of the captcha image to refresh the captcha?
I am trying to do it like this but it's not working:
<img id="captcha_img" src="<?php echo CAPTCHA_PLUGIN_URL . "/captcha_images.php?width=120&height=40&code=$code?>"?>" />
Can't read the image? click <a href='javascript: refresh_captcha();'>here</a> to refresh
<script type="text/javascript">
var refr_no = 0;
function refresh_captcha()
{
var im = new Image();
refr_no = refr_no + 1;
im.src = "<?php echo CAPTCHA_PLUGIN_URL . "/captcha_images.php?width=120&height=40"?>&refresh=" + refr_no;
document.getElementById ("captcha_img").src = im.src;
}
</script>
Upvotes: 1
Views: 897
Reputation: 13534
You don't need to create image object.
function refresh_captcha()
{
im = document.getElementById('captcha_img');
refr_no = refr_no + 1;
im.src = "<?php echo CAPTCHA_PLUGIN_URL . "/captcha_images.php?width=120&height=40"?>&refresh=" + refr_no;
}
Check out this demo: http://jsbin.com/ayenOce/2/edit
Upvotes: 1
Reputation: 2763
Try:
<script type="text/javascript">
var refr_no = 0;
function refresh_captcha()
{
refr_no = refr_no + 1;
var oldSrc = document.getElementById("captcha_img").replace('&refresh='+(refr_no-1)+'','')+"&refresh="+refr_no;
document.getElementById ("captcha_img").src = oldSrc;
refr_no++;
}
</script>
<img id="captcha_img" src="<?php echo CAPTCHA_PLUGIN_URL . "/captcha_images.php?width=120&height=40&code=$code?>"?>" />
Can't read the image? click <a href='#' onclick="refresh_captcha(); return false;">here</a> to refresh
Upvotes: 0