How to force a web browser NOT to cache images

后端 未结 17 617
太阳男子
太阳男子 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:30

    I made a PHP script that automatically adds the timestamps for all images in the HTML page. You just need to include this script in your pages. Enjoy!

    http://alv90.altervista.org/how-to-force-the-browser-not-to-cache-images/

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

    use Class="NO-CACHE"

    sample html:

    <div>
        <img class="NO-CACHE" src="images/img1.jpg" />
        <img class="NO-CACHE" src="images/imgLogo.jpg" />
    </div>
    

    jQuery:

        $(document).ready(function ()
        {           
            $('.NO-CACHE').attr('src',function () { return $(this).attr('src') + "?a=" + Math.random() });
        });
    

    javascript:

    var nods = document.getElementsByClassName('NO-CACHE');
    for (var i = 0; i < nods.length; i++)
    {
        nods[i].attributes['src'].value += "?a=" + Math.random();
    }
    

    Result: src="images/img1.jpg" => src="images/img1.jpg?a=0.08749723793963926"

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

    I checked all the answers around the web and the best one seemed to be: (actually it isn't)

    <img src="image.png?cache=none">
    

    at first.

    However, if you add cache=none parameter (which is static "none" word), it doesn't effect anything, browser still loads from cache.

    Solution to this problem was:

    <img src="image.png?nocache=<?php echo time(); ?>">
    

    where you basically add unix timestamp to make the parameter dynamic and no cache, it worked.

    However, my problem was a little different: I was loading on the fly generated php chart image, and controlling the page with $_GET parameters. I wanted the image to be read from cache when the URL GET parameter stays the same, and do not cache when the GET parameters change.

    To solve this problem, I needed to hash $_GET but since it is array here is the solution:

    $chart_hash = md5(implode('-', $_GET));
    echo "<img src='/images/mychart.png?hash=$chart_hash'>";
    

    Edit:

    Although the above solution works just fine, sometimes you want to serve the cached version UNTIL the file is changed. (with the above solution, it disables the cache for that image completely) So, to serve cached image from browser UNTIL there is a change in the image file use:

    echo "<img src='/images/mychart.png?hash=" . filemtime('mychart.png') . "'>";
    

    filemtime() gets file modification time.

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

    From my point of view, disable images caching is a bad idea. At all.

    The root problem here is - how to force browser to update image, when it has been updated on a server side.

    Again, from my personal point of view, the best solution is to disable direct access to images. Instead access images via server-side filter/servlet/other similar tools/services.

    In my case it's a rest service, that returns image and attaches ETag in response. The service keeps hash of all files, if file is changed, hash is updated. It works perfectly in all modern browsers. Yes, it takes time to implement it, but it is worth it.

    The only exception - are favicons. For some reasons, it does not work. I could not force browser to update its cache from server side. ETags, Cache Control, Expires, Pragma headers, nothing helped.

    In this case, adding some random/version parameter into url, it seems, is the only solution.

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

    I assume original question regards images stored with some text info. So, if you have access to a text context when generating src=... url, consider store/use CRC32 of image bytes instead of meaningless random or time stamp. Then, if the page with plenty of images is displaying, only updated images will be reloaded. Eventually, if CRC storing is impossible, it can be computed and appended to the url at runtime.

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

    I use PHP's file modified time function, for example:

    echo <img  src='Images/image.png?" . filemtime('Images/image.png') . "'  />";
    

    If you change the image then the new image is used rather than the cached one, due to having a different modified timestamp.

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