View Full Version : Some help with loading random images

04-09-2011, 12:19 AM
Hi all, what im trying to do is write some code that will load a random image from a folder.

The idea is to have a folder with say 10 images in it for example and then the website will load any one of the images randomly, but the image names wont be coded in, i want to be able to add an change the images without having to re program the names.

So i think i need the code to open the folder, count the images, get the names and then load one.

Any help would be greatly appreciated.

04-09-2011, 09:00 PM
There are better pieces of code out there for this, but this one is more intuitive for non-programmers. Place this where you want it on the page, & rename the image source paths, filenames & alts below as appropriate.

<SCRIPT LANGUAGE="Javascript">
function banner() { } ; b = new banner() ; n = 0
b[n++]= "<A HREF='index.html'><IMG name=randimg SRC='images/interface/banner2.jpg' border='0' ALT='Haunted House'></A>"
b[n++]= "<A HREF='index.html'><IMG name=randimg SRC='images/interface/banner3.jpg' border='0' ALT='Graves in the Snow'></A>"
b[n++]= "<A HREF='index.html'><IMG name=randimg SRC='images/interface/banner4.jpg' border='0' ALT='Insane Joker'></A>"
b[n++]= "<A HREF='index.html'><IMG name=randimg SRC='images/interface/banner5.jpg' border='0' ALT='Fangs'></A>"
b[n++]= "<A HREF='index.html'><IMG name=randimg SRC='images/interface/banner6.jpg' border='0' ALT='Screaming Clowns'></A>"
b[n++]= "<A HREF='index.html'><IMG name=randimg SRC='images/interface/banner7.jpg' border='0' ALT='Dead Servant'></A>"
i=Math.floor(Math.random() * n) ;
document.write( b[i] )