View Full Version : Background Switching Script? (non-php if possible)


Shirl
12-11-2004, 05:39 PM
Hey all, I'm looking for a simple script that allows my site visitors to select a background from a drop down menu and when they click it it changes the background with no refreshing of the page required. I've seen it done before but I can't seem to find the thread that I saw it in....anyway thanks in advance for any help. :)

Chris
12-11-2004, 09:53 PM
Here's the code, I just threw it together but it works for me. The only catch is that it only works with Internet Explorer.


<script language="JavaScript">
<!--
var backImage = new Array(); // don't change this

// Enter the image filenames you wish to use.

backImage[0] = "bg1.gif";
backImage[1] = "bg2.gif";
backImage[2] = "bg3.gif";

// Do not edit below this line.
//-----------------------------

function changeBGImage(whichImage){
if (document.body){
document.body.background = backImage[whichImage];
}
}

//-->
</script>

<form ACTION=URI>
<select name=bgchanger onchange="location.href=(form.bgchanger.options[form.bgchanger.selectedIndex].value)">
<option value="0">Change the background</option>
<option value="0"></option> (This line adds a space in your menu)
<option value="javascript:changeBGImage(0)">Background 1</option>
<option value="javascript:changeBGImage(1)">Background 2</option>
<option value="javascript:changeBGImage(2)">Background 3</option>
</select>
</form>


Change the bits in bold, and you can add as many images as you want. Also, you can customise it with colours etc. The bits in red have to be the same name.

Hope it works.
Thanks,
Chris :D

Shirl
12-12-2004, 12:26 PM
No I need it to work with all browsers. The majority of my visitors use Firefox. What about a code that generates a random background image from the images that I have?

Chris
12-12-2004, 03:45 PM
Sorry I couldn't help :(

Thanks,
Chris

Shirl
12-12-2004, 03:52 PM
Okay...eh how about a code that generates a random background image then?

Chris
12-12-2004, 04:11 PM
Well, this uses php: http://www.cb2web.com/tests/rotate.htm which I know you didn't want, but it was all i could find.

Thanks,
Chris :D

Shirl
12-12-2004, 04:14 PM
Thanks Chris :) This one looks simple. :)

Chris
12-12-2004, 08:57 PM
Welcome :D