简体   繁体   中英

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?

I can't see it anywhere in the Chrome debugger but I might be missing something.

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM