Skip to content Skip to sidebar Skip to footer

Do Browsers Cache Images?

I have a PHP page with some Javascript that changes the SRC attribute on a IMG tag after a fixed interval of time (5 seconds). There is a fixed number of images that I cycle throu

Solution 1:

Yes the browser will cache them, often even when your headers say otherwise. I have found the best combination is to issue the necessary Cache-Control headers along with appending a random string to the source URL.

Example, set the max age in seconds:

header("Cache-Control: max-age=3600");

Then append a random string to your img sources. This will need to be in Javascript, not in PHP because once your URLs are generated in PHP they won't keep updating with a new random string:

var randomString = "" + newDate().getTime();
var imageUrl = yourImageArray[someIndex] + "?" + randomString;

Hope you get the idea.

Solution 2:

If you need to try to force the browser to not cache certain images, you should properly configure the headers the server is sending for each image request such that they send the proper Cache-Control headers.

Post a Comment for "Do Browsers Cache Images?"