Dear all,
I’m building a slide show script and wanted to make it as automated as possible. To that effect I wanted to see if I could dynamically detect the number of files in the ‘gallery images’ folder so that the site owner only has to worry about adding new images to it, and not editing web pages or scripts each time. To do this I thought I could use a basic image preloading technique in a loop to count the number of images that actually exist, and then set that total as a variable that can then be used by the rest of the script. However, the event handlers (onload, onerror) seem to fire regardless of whether a file really exists or not, and in my test version I’m constantly having to artificially cap the loop so that it doesn’t run on ad-infinitum – defeating the entire point of the exercise.
My code is as follows:
[CODE]function doCount(URL) {
var y = 1;
Loop:
for (var x = 1; (x == y) && (y < 6); x++) {
tester = new Image();
tester.onload = y++;
tester.onerror = break Loop;
tester.src = URL + x + “.gif”;
}
return (x – 1);
}
var totalImages = doCount(*folder path*);
The behaviour of the event handlers has really stumped me TBH – the files don’t seem to be corrupted as they will load if added directly to a HTML image tag, and the same thing happens with other images I’ve tested with as well – and can only put it down to me either confusing how the event handlers actually work, or that they are just not supported in modern browsers as I’d expected (IE7, Firefox, Opera, etc). Does anyone have any ideas?
Thanks in advance – any help will be greatly appreciated.