Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preloading images with JavaScript

Tags:

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.

like image 809
Francisc Avatar asked Sep 05 '10 12:09

Francisc


People also ask

How do we create and preload an image in JavaScript?

Solution(By Examveda Team)The Image() constructor creates and preloads a new image object.

Can you preload images?

Starting in Chrome 73, the browser can preload the right variant of responsive images specified in srcset before it discovers the img tag! Depending on your site's structure, that could mean significantly faster image display!

How do you preload an array of images?

The Code for Preloading an Image All you need to do is modify the values in your imageArray array to be the absolute or relative path to the images you want to load: var imageArray = new Array( "images/ac3. jpg" , "images/acr.


1 Answers

Yes. This should work on all major browsers.

like image 61
Huzi--- Javiator Avatar answered Sep 21 '22 23:09

Huzi--- Javiator