I can display an image:
...
<img src="img.png">
...
So I know its on the server, but I cant simply print its name.
For example, if its in the root directory, I don’t know how to do something like:
var directory = "./"
for file in get_files(directory):
console.log(file) # should print the files in the directory
Thanks for any help.
>Solution :
JavaScript running in the browser has no way of determining what URLs are recognised by an HTTP server without either:
- Being told about them (e.g. by
<img src>) - Making an HTTP request and seeing what the response is
The is no standard HTTP feature for getting listing of URLs that are available under a given path.
Sometimes a web server will be configured so that if you ask for /foo/ then it will return an HTML document with links to every resource directly under /foo/.
If that is the case you could use the fetch api to make a request for that HTML document and then parse it, query for the links, then loop over the result and read the href attributes.
In that isn’t the case, as in the example you gave in the comments where https://tok1n.codeberg.page/fs_test/uploads/pdf/ is a 404 Not Found error page that that approach won’t work unless you are capable of changing the server to provide a directory listing.