Forum Moderators: open
If you want to this with a clienty side script like JavaScript you have 2 choices:
HTH
<html>
<head>
<title></title>
<style type="text/css">
#bgChange {
width:100px;
height:100px;
background-image:url(/images/image01.jpg);
}
</style>
<script type="text/javascript">
function random_image()
{
image_array = new Array();
image_array[0] = '/images/image02.jpg';
image_array[1] = '/images/image03.jpg';
image_array[2] = '/images/image04.jpg';
count = image_array.length;
random = Math.floor(Math.random() * count);
document.getElementById('bgChange').style.backgroundImage = "url(" + image_array[random] + ")";
}
</script>
</head>
<body onload="random_image();">
<table id="bgChange"><tr><td> </td></tr>
</body>
</html>
There can be one and only one of those id names per page, id occurs once, classes can occur repeatedly, that's how javascript knows which element is being worked with.
The tag can have both an id and a class however, there's no problem with that, it just has to have the id at least.
To use that, all you have to do is not change a thing except add more image_array[] items, just number them consecutively like they are, 0,1, 2,3,4,5... dont' skip any numbers, and it will work fine always for you.
------------
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var rand1 = 0;
var useRand = 0;
images = new Array;
images[1] = new Image();
images[1].src = "1.jpg";
images[2] = new Image();
images[2].src = "2.jpg";
images[3] = new Image();
images[3].src = "3.jpg";
function swapPic() {
var imgnum = images.length - 1;
do {
var randnum = Math.random();
rand1 = Math.round((imgnum - 1) * randnum) + 1;
} while (rand1 == useRand);
useRand = rand1;
document.randimg.src = images[useRand].src;
}
// End -->
</script>
<a onClick="swapPic();"><img name="randimg" src="whatever.jpg"></a>
---------------
What I wanted to know is..
If there is a way to add a form to the page with this script so that people can upload images they want to be included.
(I'm assuming something would have to add the new
images[xx] = new Image();
images[xx].src = "xx.jpg";
into the list at the top and I don't know how to do that.)