Skip to content
Advertisement

Preloading images with JavaScript

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();
    img.src=url;
}

I have an array of image URLs that I loop over and call the preloadImage function for each URL.

Advertisement

Answer

Yes. This should work on all major browsers.

Advertisement