如何清除或替换缓存的图像 [英] how to clear or replace a cached image

查看:113
本文介绍了如何清除或替换缓存的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多方法可以防止图像缓存(例如通过META标签),以及一些很好的技巧来确保每个页面加载时显示当前版本的图像(例如image.jpg? x = timestamp),但有没有办法在浏览器缓存中实际清除或替换图像,以便上述两种方法都不是必需的?

I know there are many ways to prevent image caching (such as via META tags), as well as a few nice tricks to ensure that the current version of an image is shown with every page load (such as image.jpg?x=timestamp), but is there any way to actually clear or replace an image in the browsers cache so that neither of the methods above are necessary?

举个例子,假设页面上有100张图像,这些图像被命名为01.jpg,02.jpg,03.jpg等。如果图像42.jpg被替换,有没有办法在缓存中替换它,以便42.jpg将在连续页面加载时自动显示新图像?我不能使用META标记方法,因为我需要永久性地将ISNT替换为保持缓存,并且我不能使用timestamp方法,因为我不希望每次页面都重新加载所有图像负载。

As an example, lets say there are 100 images on a page and that these images are named "01.jpg", "02.jpg", "03.jpg", etc. If image "42.jpg" is replaced, is there any way to replace it in the cache so that "42.jpg" will automatically display the new image on successive page loads? I can't use the META tag method, because I need everuthing that ISN"T replaced to remain cached, and I can't use the timestamp method, because I don't want ALL of the images to be reloaded every time the page loads.

我已经绞尽脑汁在互联网上寻找一种方法(最好通过javascript),但没有运气。有什么建议吗?

I've racked my brain and scoured the Internet for a way to do this (preferrably via javascript), but no luck. Any suggestions?

推荐答案

如果您正在动态编写页面,可以将最后修改的时间戳添加到URL:

If you're writing the page dynamically, you can add the last-modified timestamp to the URL:

< img src =image.jpg?lastmod = 12345678...

这篇关于如何清除或替换缓存的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆