img.src不能使用Firefox,但Chrome

时间:2015-04-15 15:56:09

标签: javascript php html

我正在尝试创建一个小型验证码来学习JavaScript。我正在使用PHP创建验证码图像,刷新图像,清除框,更改表单的颜色是由JavaScript完成的。​​

我的问题是从刷新PHP生成的图像开始。我使用'src'属性编写代码,但它不适用于Firefox。我只是可以用Chrome测试(我目前在Linux上,无法访问Windows计算机),它的工作原理。

在Firefox上,我尝试过使用静态图像。使用静态图像,它也适用于Firefox。

我的PHP代码完美运行(也不是客户端的问题)因此我的JavaScript代码会刷新图像:

function cpt_refreshCaptcha() {

            document.getElementById("cpt_theImage").src = cpt_path + "cpt_interface.php?p=img";
            console.log("src = " + document.getElementById("cpt_theImage").src);
            cpt_resetBox();

    }

我还检查了cpt_path + "cpt_interface.php?p=img部分,似乎是正确的。

Firefox的问题是什么?

2 个答案:

答案 0 :(得分:1)

这可能是缓存的问题。尝试禁用firefox浏览器的缓存。如果出现问题,您可以在文件名后添加时间戳。
请查看此信息以获取更多信息:Dynamically change image src using Jquery not working in IE and firefox

答案 1 :(得分:0)

尝试在末尾添加随机字符串以避免缓存问题:

var rand = Math.random() + new Date().getTime();
document.getElementById("cpt_theImage").src = cpt_path + "cpt_interface.php?p=img&rand=" + rand;