To read images from a directory with only javascript requires some naming convention including numbers be employed for the images. this means that all og the images must be of the same name except for differences in number. However, the script can be constructed to start at a given number and go until there are no more sequentially numbered images. So the number of images need not be specified and one can start at any number:
Code:
<script type="text/javascript">
/* ImageFindLoad Script ©2009 John Davenport Scheuer
as first seen in http://www.dynamicdrive.com/forums/
username: jscheuer1 - This Notice Must Remain for Legal Use
*/
var ims = [];
(function(){
//Configure folder and naming convention of images
var imFold = 'http://www.whatever.com/images/',
imPrefix = '',
imSuffix = '_slide.jpg',
numberingStart = 10,
numberPaddingLength = 0,
numberPaddingChar = '0',
//End configuration
imNum = --numberingStart, oops = null, im;
function numPad(n){
n += '';
while(n.length < numberPaddingLength){
n = numberPaddingChar + n;
}
return n;
}
function loadem(){
im = imFold + imPrefix + numPad(++imNum) + imSuffix;
(function(im){
var pIm = new Image();
pIm.onerror = function(){
oops = true;
alert(ims.join('\n') + '\n' + oops);
}
pIm.onload = function(){
ims.push(this.src);
setTimeout(loadem, 0);
}
pIm.src = im;
})(im);
}
loadem();
})();
</script>
As configured the above will look in:
http://www.whatever.com/images/
It will find images starting with:
10_slide.jpg
and continuing until there are no more images in sequence from that number. the images will be placed in sequence into a globally available array called ims. A side effect of this process is that all of the images found will be preloaded.
The highlighted parts are just so you can see what the result is, and may be removed. But you may use:
Code:
pIm.onerror = function(){
doWhatever();
}
to spawn other code that will do something with the images.
Other possibilities exist. With minor alteration, each time an image is found and loaded it could be displayed and a timeout interval set before looking for the next one.
Bookmarks