how to clear or replace a cached image

2019-01-11 01:00发布

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?

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.

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

17条回答
不美不萌又怎样
2楼-- · 2019-01-11 01:35

I usually do the same as @Greg told us, and I have a function for that:

function addMagicRefresh(url)
{
    var symbol = url.indexOf('?') == -1 ? '?' : '&';
    var magic = Math.random()*999999;
    return url + symbol + 'magic=' + magic;
}

This will work since your server accepts it and you don't use the "magic" parameter any other way.

I hope it helps.

查看更多
太酷不给撩
3楼-- · 2019-01-11 01:37

No, there is no way to force a file in a browser cache to be deleted, either by the web server or by anything that you can put into the files it sends. The browser cache is owned by the browser, and controlled by the user.

Hence, you should treat each file and each URL as a precious resource that should be managed carefully.

Therefore, porneL's suggestion of versioning the image files seems to be the best long-term answer. The ETAG is used under normal circumstances, but maybe your efforts have nullified it? Try changing the ETAG, as suggested.

查看更多
Deceive 欺骗
4楼-- · 2019-01-11 01:40

The reason the ?x=timestamp trick is used is because that's the only way to do it on a per image basis. That or dynamically generate image names and point to an application that outputs the image.

I suggest you figure out, server side, if the image has been changed/updated, and if so then output your tag with the ?x=timestamp trick to force the new image.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-11 01:40

In the event that an image is re-uploaded, is there a way to CLEAR or REPLACE the previously cached image client-side? In my example above, the goal is to make the browser forget what "42.jpg" is

You're running firefox right?

  • Find the Tools Menu
  • Select Clear Private Data
  • Untick all the checkboxes except make sure Cache is Checked
  • Press OK

:-)

In all seriousness, I've never heard of such a thing existing, and I doubt there is an API for it. I can't imagine it'd be a good idea on part of browser developers to let you go poking around in their cache, and there's no motivation that I can see for them to ever implement such a feature.

I CANNOT use the META tag method OR the timestamp method, because I want all of the images cached under normal circumstances.

Why can't you use a timestamp (or etag, which amounts to the same thing)? Remember you should be using the timestamp of the image file itself, not just Time.Now.
I hate to be the bearer of bad news, but you don't have any other options.

If the images don't change, neither will the timestamp, so everything will be cached "under normal circumstances". If the images do change, they'll get a new timestamp (which they'll need to for caching reasons), but then that timestamp will remain valid forever until someone replaces the image again.

查看更多
混吃等死
6楼-- · 2019-01-11 01:40

When changing the image filename is not an option then use a server side session variable and a javascript window.location.reload() function. As follows:

After Upload Complete:

Session("reload") = "yes"

On page_load:

If Session("reload") = "yes" Then
    Session("reload") = Nothing
    ClientScript.RegisterStartupScript(Me.GetType), "ReloadImages", "window.location.reload();", True)
End If

This allows the client browser to refresh only once because the session variable is reset after one occurance.

Hope this helps.

查看更多
登录 后发表回答