无法使用JS在页面上重新加载自制的验证码

Can't reload self-made captcha on the page using JS

本文关键字:加载 验证 新加载 JS      更新时间:2023-09-26
<a href="javascript:void(0);" onclick="document.getElementById("capchaimage").src="captcha.php"><img id="captchaimage" src="http://website.org/captcha.php" border="0" width="70" height="20" alt="Captcha image"></a>

Captcha图像成功加载,但不可能用上面的代码重新加载它。我做错了什么?

您的图片已被缓存。在图片URL后面的查询字符串中添加一个随机字符串+(new Date).getTime():

document.getElementById("capchaimage").src="captcha.php?"+ (new Date).getTime();

您正在用完全相同的字符串替换image src。

你可以尝试使用一些"假"参数来解决这个问题:

http://website.org/captcha.php?fake=234342412341

这样浏览器将被迫重新加载。当然每次参数都需要不同(随机的,或者只是计数器)

仔细检查你的代码:

<a href="javascript:void(0);" onclick="document.getElementById("capchaimage").src="captcha.php"><img id="captchaimage" src="http://website.org/captcha.php" border="0" width="70" height="20" alt="Captcha image"></a>
                                      ^                        ^

我很惊讶你没有得到JavaScript语法错误。我的建议是将代码移到外部函数中,以避免复杂的转义。