PDA

View Full Version : Background Change script on click able to store choices from the user in cookies



SamSP
06-27-2009, 02:30 AM
hi, im wondering how to modify the script to be able to store the decision of the user when they changes the background in the cookies


</script>
<script language="JavaScript">
<!--

var backImage = new Array();

backImage[0] = "back.jpg";
backImage[1] = "back2.jpg";
backImage[2] = "back.jpg";
backImage[3] = "";

function changeBGImage(whichImage){
document.getElementById('background').src = backImage[whichImage];
}

//-->
</script>

vwphillips
06-27-2009, 11:45 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>

<script language="JavaScript">
<!--

var backImage = new Array();

backImage[0] = "http://www.vicsjavascripts.org.uk/StdImages/One.gif";
backImage[1] = "http://www.vicsjavascripts.org.uk/StdImages/Two.gif";
backImage[2] = "http://www.vicsjavascripts.org.uk/StdImages/Three.gif";
backImage[3] = "";

function changeBGImage(whichImage){
document.getElementById('background').src = backImage[whichImage];
zxcCreateCookie('nme',whichImage,1); // change 1 to the number of days presistance required

}

function zxcCreateCookie(nme,v,days){
document.cookie=nme+'='+v+';expires='+(new Date(new Date().getTime()+days*86400000).toGMTString())+';path=/';
}

function zxcReadCookie(nme){
nme+='=';
var split = document.cookie.split(';');
for(var z0=0;z0<split.length;z0++){
var s=split[z0];
while (s.charAt(0)==' ') s=s.substring(1,s.length);
if (s.indexOf(nme)==0) return s.substring(nme.length,s.length);
}
return null;
}

function GetCookie(nme){
changeBGImage(zxcReadCookie(nme)||0);
}
//-->
</script>
</head>
<body onload="GetCookie('nme');">

<img id="background" src=" " /><br />
<input type="button" name="" value="Image 0" onclick="changeBGImage(0);"/>
<input type="button" name="" value="Image 1" onclick="changeBGImage(1);"/>
<input type="button" name="" value="Image 2" onclick="changeBGImage(2);"/>

</body>

</html>

Shammus
06-28-2009, 11:28 PM
I think this is exactly what I'm trying to do in my post as well. Would you be able to apply what you did here over there? Thank you in advance...

My post is here...
http://www.dynamicdrive.com/forums/showthread.php?t=46269

vwphillips
06-29-2009, 09:52 AM
I think this is exactly what I'm trying to do in my post as well. Would you be able to apply what you did here over there? Thank you in advance...

My post is here...
http://www.dynamicdrive.com/forums/s...ad.php?t=46269answered on your post