开发者

Using jQuery to load images, how to stop caching

I'm using jQuery to load an image dynamically on a web page. Every time a user hits submit on the page a new ima开发者_StackOverflowge is generated. The problem is, unless I append a random number at the end of the image URL, the same image is returned due to caching.

My code looks like this...

img.attr('src', url + resp.id + '.png?' + cache);

I would like to get rid of the cache string at the end and still serve a fresh image, anyone know how to do this? I don't believe it is a problem on the HTTP server side (using nginx), since making a direct request shows the correct image even without the cache buster.


That's the simplest way you can circumvent this issue, cause it's there by design.

The normal thing to do is appending the time in the string

var d = new Date();
var cache = d.getTime(); 

// getTime() Returns the number of milliseconds since midnight Jan 1, 1970
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜