Changing image src with jQuery. (Doesn't work on Firefox)

僤鯓⒐⒋嵵緔 提交于 2019-12-09 01:42:43

问题


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:

Reloading the captcha:

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

And the captcha_p.php file:

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

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);

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

Thanks!


回答1:


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

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

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

In PHP you can use microtime():

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

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());


来源:https://stackoverflow.com/questions/19542656/changing-image-src-with-jquery-doesnt-work-on-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!