Will an image with style=“display: none” still be downloaded and cached?

前端 未结 3 1617
逝去的感伤
逝去的感伤 2021-02-19 02:19

I\'m trying to figure out a way to cache the next and previous images in my gallery script ... I\'m wondering if this is a good way to do it. Also, is there any way to manually

3条回答
  •  情书的邮戳
    2021-02-19 02:36

    I’m not sure about cache behaviour with display: none (it probably varies between browsers), but you can get an image into the browser’s cache without displaying it by creating an image objects in JavaScript. The image won’t display until you add it to the page.

    var image = new Image();
    image.src = 'example.com/image'
    

    Regarding “is there any way to manually specify the cache time for the downloaded image?”, there is, but that’s dealt with in the HTTP response that delivers the image to the browser. Google has a good primer on that: https://developers.google.com/speed/articles/caching

提交回复
热议问题