Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need to make a script that will randomly choose a background image from 4 different images when the page loads and displays it, everytime it reloads it will be a different image.I think I need to use javascript and create a random number assign it to a variable then create an array of these 4 backgrounds, then create a function that chooses the array with that random number, then in the body have the onload function load it.
Here is what i have, it doesnt work.
any ideas
function get_random()
{
var ranNum= Math.floor(Math.random()*4);
return ranNum;
}var whichImage = function get_random();
var back=new Array(4)
back[0]="bg1a.jpg";
back[1]="bg2a.jpg";
back[2]="bg3a.jpg";
back[3]="bg4a.jpg";
</script></head>
<body onload="javascript:back[whichImage]">

I figured it out, just in case anyone needs the script,
<SCRIPT LANGUAGE="JavaScript">
random_number= Math.floor(Math.random()* 4+1 );
// Loads the appropriate image and text color based on random number.
if (random_number==1) {
image="bg1a.jpg";
}
if (random_number==2) {
image="bg2a.jpg";
}
if (random_number==3) {
image="bg3a.jpg";
}
if (random_number==4) {
image="bg4a.jpg";
}// End -->
</SCRIPT>
</head><body onload="document.body.background = image">
</body>
</html>

Hi sorry to be a pain and all but is it possible to incorporate this code into your current code? http://answers.yahoo.com/question/index?qid=20070616132155AAicr6Y

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |