0

Scenario: I am creating a personal home page that i plan for using in safari/chrome/Firefox and I have a folder with images. I want to display a random image as the background of the page.

My solution to this was to create an array with all the images as separate links as the objects of the array. My JS works with a pre-made array of links but I have no clue on how to create an array from a directory.

I don't mind using jquery if required but i cant have server side php (or anything else) since i am linking the local html file to the new tab page.\

The html file is in the directory / and the images in /img .

Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158
Yash A
  • 3
  • 6
  • Welcome to SO please take a tour of the [help centre](http://stackoverflow.com/help) to see [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and what types of question are [on topic](http://stackoverflow.com/help/on-topic) for the site – Pete Apr 11 '17 at 12:38
  • The only way to do what you want though is to make an ajax request to a serverside php file - js is clientside and cannot read the file structure of the server – Pete Apr 11 '17 at 12:40
  • please refer http://stackoverflow.com/questions/29340664/javascript-retrieve-names-of-files-in-a-folder – Nalin Savaliya Apr 11 '17 at 12:42
  • Possible duplicate of [Does Javascript support the ability to get a directory listing?](http://stackoverflow.com/questions/4913746/does-javascript-support-the-ability-to-get-a-directory-listing) –  Apr 11 '17 at 19:24

1 Answers1

1

This can't be done. You're going to need something on the server side to read the directory that is on your server. Javascript (including jQuery) running on the user's browser doesn't have access to list directories on your server.

Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158