View Single Post
07-20-2008, 08:24 PM
#10
infinivert is offline infinivert
infinivert's Avatar
Status: Junior Member
Join date: Jul 2008
Location: Abilene TX
Expertise: Design, PHP, JS, HTML5, CSS3
Software:
 
Posts: 37
iTrader: 0 / 0%
 

infinivert is on a distinguished road

Send a message via AIM to infinivert

  Old

Still, though, this is the javascript forum, and all the advice given has been PHP (which I DO think is the better option). But since you don't want to use PHP, you'd probably do something like this in your page's HEAD section:

Code:
<script type="text/javascript">

function randomimage()
{
  var path = "/path/to/images/folder/"; /* your image path goes here */

  var images = new Array();
  images[0] = "image1.jpg";
  images[1] = "image2.jpg";
  images[2] = "image3.jpg";
  images[3] = "image4.jpg"; /* etc. for all your image files */

  var randint = Math.round( Math.random() * ( images.lenth - 1 ) ); /* gives you a random integer between 0 and the key of the final image specified (in this case 3), inclusive */

  var randimage = path+images[randint]; /* specifies one of your images at random */

  document.getElementById("MyDivID").style.background-image = randimage; /* sets the background of the element with the id set to "MyDivID" to the random image chosen above */
}

</script>
Then modify your opening BODY tag to look like this:

Code:
<BODY onload="randomimage()">
This code has not been tested, so it may have some errors, but hopefully it will give you the general idea of what you're going for here.

Hope this helps!

--Josh