如何强制chrome不重新加载图像与相同的url,直到页面刷新,像firefox


how to force chrome not to reload images with same url till the page is refreshed ,like firefox

Firefox在刷新之前不会重新加载具有相同url的图片。

$("#a").click(function(){$("#img").attr("src","img.php?id=3");});
<img id="img" src="img.php?id=3">
//the two line above are in the same html page.And when the html page is loaded
// in the browser, i open a new tab and go to the php page that includes 
//the line below.
mysql_query("update images set data=$data where id=3");//changing the image here

然后我回到HTML页面并单击#a,在firefox中显示旧图像,因此它不会询问mysql是否更改了id=2的图像,除非HTML页面刷新。但是在chrome中显示的是新的、改变过的图像。它会再次询问mysql,不关心url是否仍然相同。

我希望chrome的行为像firefox。尝试为不缓存设置过期,最后修改等头,但它以不同的方式工作。据我所知,它没有缓存我要找的东西。无论如何,这对我来说是一个非常重要的问题,我将感谢任何帮助。

使用这个;

<script>
var randomNumber = Math.random();
$("#a").click(function(){$("#img").attr("src","img.php?id=3&rand=" + randomNumber);});
</script>
<img id="img" src="img.php?id=3&rand="<script>document.write(randomNumber)</script>>

这将总是生成不同的url。你不需要在后端处理rand php

浏览器行为因人而异。有些人将浏览器设置为保存历史记录并缓存,有些人则没有。

您可以通过设置页面标题来鼓励或阻止缓存,如下所示:

http://www.php.net/manual/en/httpresponse.setcachecontrol.php

注意,这将被本地浏览器设置覆盖。

另外,通过创建一个不包含查询字符串的url,例如:

http://www.mydomain.com/myimage3/vs. http://www.mydomain.com/image.php?id=3

你"可能"有更好的运气鼓励在浏览器中缓存,虽然我不再是这样的情况下,因为它曾经是。

(像zend和codeigniter这样的框架以更容易部署的格式提供不带查询字符串的url结构)