简体   繁体   English

浏览器何时缓存下载的图像?

[英]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.

相关问题 仍然会下载并缓存带有style =“display:none”的图像吗? - Will an image with style=“display: none” still be downloaded and cached? 我如何知道图像在加载时缓存在浏览器中 - how do I know the image is cached in browser when do the loading 如何防止浏览器使用缓存的图像? - How to prevent the browser using a cached image? 离线时浏览器会加载缓存的图像吗? - Are cached images loaded by the browser when offline? 下载文件时的浏览器事件保存到磁盘 - Browser event when downloaded file is saved to disk 高速缓存时,图像宽度跟踪为零 - Image width traces zero with onload when cached 如何使浏览器显示的图像具有相同的名称而不是已缓存的图像? - How to make the browser display image with same name and not the cached one? 如何检查浏览器是否缓存了特定资源(例如图像)? - How to check if a particular resource (e.g. image) is cached by the browser? 如何在浏览器中显示从云存储下载的图像? - How to display image downloaded from cloud storage in the browser? 在显示图像之前确定图像是否缓存在浏览器中(memory 或磁盘缓存)? - Determine if an image is cached in browser (either memory or disk cache) before displaying the image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM