views:

78

answers:

4

Hi,

Is it possible to iterate through a collection of files in Javascript? I am writing a jQuery plugin that takes either an array of images or, I was hoping, a directory containing a list of images. E.g. either:

['image1.jpg','image2.jpg','image3.jpg']

or 'http://somedomain.com/images/'

I would then like to be able to iterate through the above domain and take the file name from each of the files in the folder.

I have read about the FileSystemObject but this will only be available in IE, is there an equivalent that can be used in all browsers?

+3  A: 

You should use server side language to provide array of images in JSON or similar. There is no way Javascript can access local files. That would be serious security issue.

dusoft
Agreed. I'd vote up if I could! :)
Alex
A: 

You can't.

You have to get the list from your server through JSON or XML. Because of this, you can't do this in any way with javascript on servers you have no control over. Also, access to client side files is actively avoided for security reasons.

voyager
A: 

You would need to create a web service to fetch the list of files. You can't use ajax to directly fetch the directory listing because ajax calls are restricted to the domain.

John Himmelman
A: 

If the directory is on the same domain, and your webserver is setup to show directory indexes, then you could use javascript to request the url and load it into the dom for data extraction. This is brittle, and doesn't sound like it would make for a good plugin, much less good for anything but a one off task. But, you could do it.

The other suggestions of having serverside script output json or xml would be an improvement.

chris