View Full Version : random images

07-08-2005, 09:24 AM
okay this should be quite simple but I'm already confused. Is there a way to put all necessary pictures in a folder and then place an image to html and when the image is clicked it would open random image from the folder. So I could easily add images afterwards?

07-08-2005, 10:11 AM
You can use either javascript or PHP to call in the images and display them randomally. If you want to use PHP, you'd have to use the mt_rand() function ;)

07-08-2005, 10:29 AM
If you want it to where the image changes when the user clicks the image then you would need to use javascript using an array to store the images like so and call them randomly onclick.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
var theImages = new Array()
theImages[0] = '100.jpg'
theImages[1] = '200.jpg'
theImages[2] = '300.jpg'
theImages[3] = '400.jpg'

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
preBuffer[i] = new Image()
preBuffer[i].src = theImages[i]
function showImage(){
var whichImage = Math.round(Math.random()*(p-1));


<img src="100.jpg" onclick="showImage()" id="theimage" alt="">
If you want it to where onload it displays a random image then I would use php. Here (http://automaticlabs.com/products/rotator) is an example that reads the images from a directory and picks a random image to display.