Swap Your Background With The Greatest Of Ease!

I thought it might be nice to share the script that I am using to change out my background image. As you probably have noticed, when you mouse over the images it not only changes the image but also remembers your choice on the next page.

This script is simple enough to use and you can start by downloading the JavaScript by right clicking on the following file and saving it to your local drive. bg.js

Here is the source if you wish to use this code in a single page.

var backImage = new Array();
backImage[0] = "img/bg1.gif";
backImage[1] = "img/04original.jpg";
backImage[2] = "img/02original.gif";
backImage[3] = "img/06original.jpg";
backImage[4] = "img/35original.gif";
var i;
for(i in backImage)
{
imgPath = backImage[i];
backImage[i] = new Image;
backImage[i].src = imgPath;
}
function changeBGImage(whichImage){
if (document.body){
document.body.background = backImage[whichImage].src;
setCookie('bgnum',whichImage);
}
}
function setBGDefault(){
var defBG = getCookie('bgnum');
if (defBG != null) {
changeBGImage(defBG);
} else {
changeBGImage(0);
}
return true;
}
function setCookie(name, value, expires, path, domain, secure)
{
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
function getCookie(name)
{
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1)
{
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1)
{
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain)
{
if (getCookie(name))
{
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}