How to force a web browser NOT to cache images

后端 未结 17 615
太阳男子
太阳男子 2020-11-22 13:51

Background

I am writing and using a very simple CGI-based (Perl) content management tool for two pro-bono websites. It provides the website administrator with HTML

相关标签:
17条回答
  • 2020-11-22 14:23

    You must use a unique filename(s). Like this

    <img src="cars.png?1287361287" alt="">
    

    But this technique means high server usage and bandwidth wastage. Instead, you should use the version number or date. Example:

    <img src="cars.png?2020-02-18" alt="">
    

    But you want it to never serve image from cache. For this, if the page does not use page cache, it is possible with PHP or server side.

    <img src="cars.png?<?php echo time();?>" alt="">
    

    However, it is still not effective. Reason: Browser cache ... The last but most effective method is Native JAVASCRIPT. This simple code finds all images with a "NO-CACHE" class and makes the images almost unique. Put this between script tags.

    var items = document.querySelectorAll("img.NO-CACHE");
    for (var i = items.length; i--;) {
        var img = items[i];
        img.src = img.src + '?' + Date.now();
    }
    

    USAGE

    <img class="NO-CACHE" src="https://upload.wikimedia.org/wikipedia/commons/6/6a/JavaScript-logo.png" alt="">
    

    RESULT(s) Like This

    https://example.com/image.png?1582018163634
    
    0 讨论(0)
  • 2020-11-22 14:24

    Add a time stamp <img src="picture.jpg?t=<?php echo time();?>">

    will always give your file a random number at the end and stop it caching

    0 讨论(0)
  • 2020-11-22 14:26

    Armin Ronacher has the correct idea. The problem is random strings can collide. I would use:

    <img src="picture.jpg?1222259157.415" alt="">
    

    Where "1222259157.415" is the current time on the server.
    Generate time by Javascript with performance.now() or by Python with time.time()

    0 讨论(0)
  • 2020-11-22 14:26

    I'm a NEW Coder, but here's what I came up with, to stop the Browser from caching and holding onto my webcam views:

    <meta Http-Equiv="Cache" content="no-cache">
    <meta Http-Equiv="Pragma-Control" content="no-cache">
    <meta Http-Equiv="Cache-directive" Content="no-cache">
    <meta Http-Equiv="Pragma-directive" Content="no-cache">
    <meta Http-Equiv="Cache-Control" Content="no-cache">
    <meta Http-Equiv="Pragma" Content="no-cache">
    <meta Http-Equiv="Expires" Content="0">
    <meta Http-Equiv="Pragma-directive: no-cache">
    <meta Http-Equiv="Cache-directive: no-cache">
    

    Not sure what works on what Browser, but it does work for some: IE: Works when webpage is refreshed and when website is revisited (without a refresh). CHROME: Works only when webpage is refreshed (even after a revisit). SAFARI and iPad: Doesn't work, I have to clear the History & Web Data.

    Any Ideas on SAFARI/ iPad?

    0 讨论(0)
  • 2020-11-22 14:26

    Your problem is that despite the Expires: header, your browser is re-using its in-memory copy of the image from before it was updated, rather than even checking its cache.

    I had a very similar situation uploading product images in the admin backend for a store-like site, and in my case I decided the best option was to use javascript to force an image refresh, without using any of the URL-modifying techniques other people have already mentioned here. Instead, I put the image URL into a hidden IFRAME, called location.reload(true) on the IFRAME's window, and then replaced my image on the page. This forces a refresh of the image, not just on the page I'm on, but also on any later pages I visit - without either client or server having to remember any URL querystring or fragment identifier parameters.

    I posted some code to do this in my answer here.

    0 讨论(0)
  • 2020-11-22 14:30

    I would use:

    <img src="picture.jpg?20130910043254">
    

    where "20130910043254" is the modification time of the file.

    When uploading an image, its filename is not kept in the database. It is renamed as Image.jpg (to simply things out when using it). When replacing the existing image with a new one, the name doesn't change either. Just the content of the image file changes.

    I think there are two types of simple solutions: 1) those which come to mind first (straightforward solutions, because they are easy to come up with), 2) those which you end up with after thinking things over (because they are easy to use). Apparently, you won't always benefit if you chose to think things over. But the second options is rather underestimated, I believe. Just think why php is so popular ;)

    0 讨论(0)
提交回复
热议问题