This page demonstrates how to use JavaScript to generate a random background image whenever the page is loaded or refreshed. You obviously have to be careful when choosing the background images to ensure that whichever is chosen the text can still be read.

To produce this effect, in the HEAD section of the page put...

<script language="javascript">
<!-- Hide the script from old browsers
function showimage(){
//Define and populate the array
var images = new Array;
images[0]="bkg89.jpg";
images[1]="tile079.jpg";
images[2]="tiedye.gif";
images[3]="back1.jpg";
//Generate a random number then display the image from that array index
rdmImage = Math.floor(Math.random()*images.length);
document.body.style.backgroundImage = 'url(' + images[rdmImage] + ')';
}
-->
</script>

Change the BODY tag to read...

<body onload=showimage()>

This page created 22nd October 2004, last modified 24th October 2004


GoStats stats counter