[英]When Is a Downloaded Image Cached by the Browser?
Does this script cause the image to be cached by the browser? 此脚本会导致图像被浏览器缓存吗?
var img = new Image();
img.src = "http://www.nasa.gov/sites/default/themes/NASAPortal/images/nasa-logo.gif"
Or will it be actually downloaded/cached only when added to the the DOM? 还是仅在添加到DOM时才实际下载/缓存它?
I can't see it anywhere in the Chrome debugger but I might be missing something. 我在Chrome调试器中的任何位置都看不到它,但可能会丢失一些东西。
Thanks 谢谢
Assuming there are no http headers instructing the browser to avoid caching, the image will be cached as soon as it is successfully downloaded, no matter whether it was downloaded in a javascript object, put in the DOM with JS or part of the page's HTML. 假设没有指示浏览器避免缓存的http标头,则无论成功下载的图像是javascript对象,带JS的DOM还是页面HTML的一部分,图像都会在成功下载后立即进行缓存。 That part makes no difference.
那部分没有区别。
In fact, you can even preload images (into the cache) with pure javascript like this: How do you cache an image in Javascript . 实际上,您甚至可以像这样使用纯JavaScript将图像预加载到缓存中: 如何在Javascript中缓存图像 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.