Forums
This topic is locked
Random image from a folder
07 Mar 2009 17:25:30 Helle M posted:
Does anybody know where to get a code that will show one different image everytime the page loads. Images should come from a specific folder that I choose ....The site does not support any database, that is why I need this script.
thanks Helle
Edited by - Helle M on 07 Mar 2009 17:26:28
Replies
Replied 20 Mar 2009 21:53:39
20 Mar 2009 21:53:39 Georgi Kralev replied:
Hi Helle,
There is a random JavaScript function which could be used for this purpose.
The following example gives sample solution to your issue. It shows a random image from five images every time the page loads. It looks for the images in image folder and expects the images to have the following names: image_0.jpg, image_1.jpg etc.
Hope this helps.
There is a random JavaScript function which could be used for this purpose.
The following example gives sample solution to your issue. It shows a random image from five images every time the page loads. It looks for the images in image folder and expects the images to have the following names: image_0.jpg, image_1.jpg etc.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Random Image</title> </head> <body> <img src="images/image_0.jpg" align="middle" border="0" name="RandomImg" > <script language="JavaScript"> <!-- // Genarate random value from 0-5 var rand_no = Math.floor(6*Math.random()); // This defines the source of the preview image (For example images/image_0.jpg) document.images['RandomImg'].src="images/image_" + rand_no + ".jpg"; // --> </script> </body> </html>
Hope this helps.