0

I am trying to build a simple website with just an index file and a folder full of icons for our internal purpose. I am making this so that anyone in our team can just access the link and download the icons whenever needed without our help. I am trying to read the icons file name from the folder automatically, so every time we update the folder with new ones we don't have to edit the HTML. I know only JavaScript. Please help me.

8
  • 4
    not going to happen with JavaScript in the browser. Commented Sep 27, 2017 at 13:47
  • @epascarello I don't know, nodejs & express? :) Commented Sep 27, 2017 at 13:50
  • 1
    When you update run a small bat/script to list out the names to a file (something like ls path > list.txt), then read/parse that file from js instead. You won't have access from js to list the files directly. This is such a elegant solution I have awarded myself a glass of soda. Commented Sep 27, 2017 at 13:58
  • 1
    @freginold assuming it ends up in the same folder as the images or as the index.html, you can read it using fetch() or XMLHttpRequest. Commented Sep 27, 2017 at 14:28
  • 1
    @K3N Sorry, brain fart. You should write that up as an answer. Commented Sep 27, 2017 at 14:46

2 Answers 2

2

An alternative could be to produce a filelist of the files via a script or batch file (depending on OS, something like - in psudeo - ls path/to/images > filelist.txt) which redirect the output to a file that ends up in the same root as the index and images.

This file could then be read via fetch() or XMLHttpRequest() and parsed on client side to provide the basis for the links to the updates files.

This would require an extra step in the pipe-line of course, but could easily be automated using a listener for the image directory that triggers the script, part of your project setup and so forth.

Sign up to request clarification or add additional context in comments.

Comments

1

This is not possible. In your case Javascript is running in the browser (client-side). It has no access to the servers filesystem. You need a server-side dynamic web-service (php, node.js, ...)

4 Comments

Why wouldn't the solution @K3N proposed in the comments above (save file names to txt file, read it in w/ JS) work?
Yes. This could be a solution. But then they would have to change the text-file each time they are changing files in their servers filesystem.
True, but could be done with an automated script/task.
Yeah. He got an upvote from me for this dirty but effective solution ;-) But I think that it would be easier to extend the existing static html-files with dynamic php-code-snippets for reading the filesystem and providing link generation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.