Is there any way to loop through a directory full of images (or any type of file, for that matter) with Javascript?
EDIT: The directory is on the server. Sorry for the confusion.
Is there any way to loop through a directory full of images (or any type of file, for that matter) with Javascript?
EDIT: The directory is on the server. Sorry for the confusion.
Share Improve this question asked Jan 20, 2011 at 21:51 Mark SzymanskiMark Szymanski 58.2k24 gold badges72 silver badges87 bronze badges 1- Is the javascript running in a client's browser, and is the directory on the server? Perhaps you should give more detail about what you're hoping to acplish. – user113716 Commented Jan 20, 2011 at 21:54
3 Answers
Reset to default 2No.
If there was, it would've been a major security problem : anyone could have gain access to (your) the server's hard-disk through any browser.
It is possible to implement a Directory/File parser in other server-side languages and return its result through ajax.
JavaScript does not have access to the file system directly. If you do not know what the names of the files are you have to generate the list with some higher level scripting language like PHP.
As I say that, there is an off chance that, if you had directory browsing enabled on the directory in question, that you could attempt to retrieve that directory via an AJAX call and then parse the results. I haven't ever tried, but it may be possible.
You can't do directory actions directly through javascript. What you can do though is name your files in order and check based on the name. Like if your names are Image1.png, Image2.png you can make a js loop that creates Image objects based on that number increment and check if it loaded using an onload function.