How to prevent caching (force image reload) wtih PHP and JavaScript/jQuery
I tried these two method to prevent caching (force image reload) wtih PHP and JavaScript/jQuery, but I still get the image from the cache when I load the content through ajax,
PHP:
// Generate a number that wil开发者_StackOverflow中文版l never be repeated using the time function
// that "returns the current time measured in the number of seconds since
// the Unix Epoch (January 1 1970 00:00:00 GMT)"
$cachekiller = time();
// Include the generated number in the image URL
<img src="path/to/image.png?{$cachekiller}"
Javascript/ jquery:
// Generate random number between 1 and 1000.
var cachekiller = Math.floor(Math.random()*1001);
$("#thumbnail").attr("src", "path/to/image.png?"+cachekiller);
What else can I do?
I am assuming that the image changes frequently in the server and you want to reload it through ajax. If that is the case, adding a random number to the URL will not help. This is because, servers do not apply cache control for resources based on URL. They use the "If-Modified-Since" parameter in the request headers for cache control. Refer this. You can try setting the If-Modified-Since header in the request to a past time and see if that helps.
精彩评论