Preloading images with JavaScript

后端 未结 14 1229
太阳男子
太阳男子 2020-11-22 03:17

Is the function I wrote below enough to preload images in most, if not all, browsers commonly used today?

function preloadImage(url)
{
    var img=new Image(         


        
14条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 03:40

    I can confirm that the approach in the question is sufficient to trigger the images to be downloaded and cached (unless you have forbidden the browser from doing so via your response headers) in, at least:

    • Chrome 74
    • Safari 12
    • Firefox 66
    • Edge 17

    To test this, I made a small webapp with several endpoints that each sleep for 10 seconds before serving a picture of a kitten. Then I added two webpages, one of which contained a

提交回复
热议问题