使用jQuery更改图片src. (不适用于Firefox) [英] Changing image src with jQuery. (Doesn't work on Firefox)

查看:109
本文介绍了使用jQuery更改图片src. (不适用于Firefox)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Ajax联系表格.我也写了我自己的验证码.但是我有刷新图像的问题.我这样写:

I'm writing an Ajax contact form. I have written my own captcha too. But I have a problem about refreshing the image. I have written this like that:

重新加载验证码:

<code>$("#captchaSection").load("captcha_p.php");</code>

还有captcha_p.php文件:

And the captcha_p.php file:

<code>< img src="captcha.php" name="imgCaptcha" /></code>

并且我已将以下行添加到capcha.php:

And I have added this lines to the capcha.php:

header("Cache-Control: no-cache, no-store, must-revalidate");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

它在Google Chrome和Safari上完美运行.但不适用于Firefox和资源管理器.

It works perfect on Google Chrome and Safari. But doesn't work on Firefox and Explorer.

谢谢!

推荐答案

似乎Firefox和IE正在缓存图像.为防止这种情况,请将时间戳记附加到URL和图像源:

It seems that Firefox and IE are caching the image. To prevent this, append a timestamp to the URL and image source:

在Javascript中,您可以使用new Date().getTime():

In Javascript you can use new Date().getTime():

$("#captchaSection").load("captcha_p.php?" + new Date().getTime());

在PHP中,您可以使用microtime():

In PHP you can use microtime():

< img src="captcha.php?<?php echo microtime(); ?>" name="imgCaptcha" />

我看不到使用.load()加载包含图像的HTML的任何好处.仅更改图像的src属性会更容易,例如:

I don't see any benefit of using .load() to load HTML that contains the image. It would be easier to just change the src property of the image, for example:

// refresh captcha
$('img[name=imgCaptcha]').prop('src', 'captcha.php?' + new Date().getTime());

这篇关于使用jQuery更改图片src. (不适用于Firefox)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆