Skip to main content
Nancy OShea
Community Expert
Community Expert
September 1, 2015
Answered

jQuery Preload folder contents?

  • September 1, 2015
  • 2 replies
  • 2414 views

I'm using a jQuery image preloader:

    $.preloadImages = function() {

  for (var i = 0; i < arguments.length; i++) {

$("<img />").attr("src", arguments);

  }

}

$.preloadImages("hoverimage1.jpg","hoverimage2.jpg");

Problem: Client will be using different images throughout the year.

Question: Instead of listing an array of filenames above, is it possible to preload everything contained in a folder?

Nancy O.

This topic has been closed for replies.
Correct answer David_Powers

I don't know if it's possible with JavaScript, but generating an array of image filenames from a folder is easy in PHP:

$filenames = [];

$images = new FileSystemIterator('path/to/folder');

$images = new RegexIterator($images, '/\.(?:jpg|png|gif)$/i');

foreach ($images as $image) {

   $filenames[] = $image->getFilename();

}

You could then use the $filenames array to inject the names into your jQuery preloader script.

2 replies

David_Powers
David_PowersCorrect answer
Inspiring
September 2, 2015

I don't know if it's possible with JavaScript, but generating an array of image filenames from a folder is easy in PHP:

$filenames = [];

$images = new FileSystemIterator('path/to/folder');

$images = new RegexIterator($images, '/\.(?:jpg|png|gif)$/i');

foreach ($images as $image) {

   $filenames[] = $image->getFilename();

}

You could then use the $filenames array to inject the names into your jQuery preloader script.

Nancy OShea
Community Expert
Community Expert
September 2, 2015

I like your solution David.  PHP to create the array, jQuery to preload.  Makes a lot of sense.

Thanks.

Nancy O.

Nancy O'Shea— Product User & Community Expert
kglad
Community Expert
Community Expert
September 2, 2015

that's also in the link i posted.

kglad
Community Expert
Community Expert
September 2, 2015