admin管理员组文章数量:1394175
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:
- To load from cache if the image were the same.
- 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:
- To load from cache if the image were the same.
- 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 badges3 Answers
Reset to default 5What'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()
本文标签: javascriptCaching dynamically loaded imagesStack Overflow
版权声明:本文标题:javascript - Caching dynamically loaded images - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744688904a2619869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论