View Full Version : Changing background randomly in sync with image

Mar 26th, 2007, 11:05 PM
I would like to create a page where certain areas with backgrounds would randomly change as the page is refreshed.

I know this is possible with images, but Im not sure about the background, as this would be CSS?

However, I would like this change to be in sync with the image too.

For example. The page background is orange, the image should also be orange. The page is refreshed, the background is now blue, the image should now be blue. See what I mean?

Is this possible?

Mar 27th, 2007, 08:51 PM
<script type="text/javascript">
function changeBkground(){
var colors = new Array("orange", "blue", "red", "yellow", "green");
var index = Math.floor(Math.random()*colors.length);
document.body.style.backgroundColor = colors[index];
document.getElementById("color").src = colors[index] + ".gif";
<body onload="changeBkground()">
<img id="color" src="default.gif" alt="" />

Mar 27th, 2007, 10:32 PM
No way!

I was having so much trouble trying to write one using PHP, and you just did it...just like that!

Thank you!

Apr 8th, 2007, 10:11 PM
Sorry, this is a bit late. I've only just round implementing this on my website.

Could you please explain what variables I have to change within the code? And how it works?