Click to See Complete Forum and Search --> : Find files w/ JS


freefall
06-09-2003, 12:16 PM
Is there a way, or what is the best way to find out the names of files in a certain directory using JS?

I have recently made a JS gallery on my website which loads images based on the prefix of their name and their number (ie church1, church2, etc.). I want to make the gallery automatically look to see how many such images are in the directory. Currently I pass the number I want it to display in the url (ie gallery.html?church=5 or gallery.html?pioneer_club=3). That's no problem to me, I just have to update the main page that links to the gallery every time I add images, it'd just be that much easier if it were automatic.

Thanks for your help, if you need to look at it more closely, the gallery is at www.hambdenalliance.org/gallery. The gallery.html file is in hambdenalliance.org/gallery and the images are in /gallery/images.

~ Ian

Khalid Ali
06-09-2003, 12:46 PM
JavaScript does not have the ability to read write files in such manner,however there might be a hack that you can implement.
Use JS array to preload images and store them in the array.
JavaScript arrays are like collections in java where you may or may not give it an initial size.Array will inflate according to the number of records in it.

So what you may want to do is
make use of image object's

onerror method.

What it doesis that if an image is not download/preloadable it throws an error,and you can catch that error to stop preloading more images.

For this to work you have to make sure that all of theimages in the folder have the legal names so that a preloading of an image does not throw an error while there are allots more images to be preloaded.

freefall
06-09-2003, 01:36 PM
Hey, thanks a lot! Now if I load the thumbnails into an array in a while loop, I can simply keep checking the numbers till it finds one that doesnt return an image, then whatever my counter variable's at is how many images to load, now I would probably do this on an intermediate page, because the number of images is written into the gallery.html as soon as its opened.
Can you just give me an example of the onerror, does it go in the image tag or would it be the condition in the while loop?

Thanks a lot,
Ian

freefall
06-09-2003, 01:56 PM
okay, i just did a little research. so the onerror event stems from the window object, so that would handle any error?
i'm guessing then that i just put
window.onerror = break;
in my loop that's checking the images? that's really easy.
Thanks, if I'm wrong here, maybe you can straighten me out but I think I'll go try this out now.
~ Ian

Khalid Ali
06-09-2003, 02:02 PM
You are welcome..BTW
here is a link where I have implemented image.onerror functionality.
http://68.145.35.86/skills/javascripts/FindOutAServerIsUp.html