How to force chrome not to reload images with the same url until the page refreshes like firefox

Firefox does not reload images with the same URL before refreshing.

$("#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

      

then i go back to html page and press #a, firefox shows old image, so it doesn't ask mysql if image with id = 2 is changed or not if html page is not refreshed. But in chrome a new, modified image is shown. Goes and asks mysql again, don't care if the url is another one.

I want chrome to behave like firefox.Tried to set expire, last modified, etc. headers for caching, but it worked differently. As I understand it, it does not cache what im looking for. a really important issue for me, I would appreciate any help.

0


source to share


2 answers


Use this;

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

      



This will always generate a different url. You don't need to process rand

in the php backend

+1


source


Browser behavior is unique to individual users' browsers. Some people have found that their browsers keep history and therefore cache, others don't.

You can encourage or discourage caching by setting headers on the page, as shown here:

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

Please note: this is redirected by your local browser settings.



Also, by creating a url that doesn't include the request, for example:

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

"You may have better luck encouraging browser caching, although I won't be the same anymore."

(frameworks like zend and codeigniter offer request-free url structures in a more deployable format)

0


source







All Articles