I'm very new to HTML and want to create my own website. On my website I want a button that when pressed returns a random image stored on the server. This image needs to be displayed on the same page. From several sources i found code that looks like this:
function pickimg(){
var imagenumber = 5 ;
var randomnumber = Math.random() ;
var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1;
images = new Array
images[1] = "IMAGE #1"
images[2] = "IMAGE #2"
images[3] = "IMAGE #3"
images[4] = "IMAGE #4"
images[5] = "IMAGE #5"
var image = images[rand1]
document.randimg.src = image
}
The problem with this approach is that the list of files needs to be determined beforehand. The list of different pictures is very long and it seems inefficient to make a separate entry for each file. My javaScript skills are non-existent but i do know some python.
This code wil add every filename in my folder to a list just like the JavaSCript Example, although it's now automated.
pictlist = []
path = "website\\pictures"
files = os.listdir(path)
for picture in files:
pictlist.append(picture)
My question is then, How can i translate this python code into JavaScript and what HTML code do i need to add to the tag?
Thanks!