最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Caching dynamically loaded images - Stack Overflow

programmeradmin1浏览0评论

I'm using javascript to dynamically load any of a series of images into a single img tag, based on user interaction:

function reassignImage(newSource)
{
   img.src = newSource;
}

This works great, except that I when I inspect it with Chrome developer tools, I see that even if I reload an image I've already loaded, it makes another http call AND grows the total Images Size graph.

This seems like the worst of both worlds. I would want either:

  1. To load from cache if the image were the same.
  2. To reload each image everytime, but then not grow the cache.

How would I achieve either scenario?

Thanks! Yarin

I'm using javascript to dynamically load any of a series of images into a single img tag, based on user interaction:

function reassignImage(newSource)
{
   img.src = newSource;
}

This works great, except that I when I inspect it with Chrome developer tools, I see that even if I reload an image I've already loaded, it makes another http call AND grows the total Images Size graph.

This seems like the worst of both worlds. I would want either:

  1. To load from cache if the image were the same.
  2. To reload each image everytime, but then not grow the cache.

How would I achieve either scenario?

Thanks! Yarin

Share Improve this question asked Sep 14, 2010 at 19:31 YarinYarin 184k155 gold badges410 silver badges524 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

What's the cache-control header set to in the http response for the image? (Chrome developer tools will show you). If it's not set to be cacheable, it will get refetched.

This will pre-load an image so that the browser can display it immediately when you actually set the src of an img tag. I speculate that pre-loading an image like this will ensure it's in the cache so it won't reload, though I haven't tested it.

var myImg = new Image(25, 25);
myImg.src = "/foobar.png";

In other words, this should now hopefully only download two images

function reassignImage(newSource) {
    var myImg = new Image(25, 25);
    myImg.src = newSource;
    img.src = newSource;
}

reassignImage("first.png");
reassignImage("second.png");
reassignImage("first.png");

Edit

I was doing it wrong. Try creating a new Image() for every new file the user loads. Swap these image elements in and out of the dom.

<html>
  <head>
    <script>
    var imageElements = {};
    function reassignImage(newSource) {
      if (typeof imageElements[newSource] == "undefined") {
        imageElements[newSource] = new Image();
        imageElements[newSource].src = newSource;
      }
      var container = document.getElementById("imageContainer");
      container.innerHTML = '';
      container.appendChild(imageElements[newSource]);
    }
    </script>
  </head>
  <body>
    <div id="imageContainer"></div>
  </body>
</html>

Technically you can set it this way in .htaccess file:

# Set up caching on images for 1 month
<FilesMatch "\.(jpg|jpeg|png|ico|gif)$">
ExpiresDefault A2419200
</FilesMatch>

If you would like with this settings force image to refresh, append this to its URL:

'?'+ new Date().getTime()
发布评论

评论列表(0)

  1. 暂无评论