...

View Full Version : Hello! Need help keeping cookies in script!



normpeterson
05-05-2004, 07:26 PM
:confused:

Hello all! I have this script that changes the color of the page, but I cant get the cookie to change the color to what the user selected when they return to the page. The cookie gets put in the cookie file, but thats it. Do I need another function besides setCookie to keep the cookie there? Thanks for any help! Norman. :)

<html>
<head>

<title>Color change cookies</title>

<script>
function setCookie()
{
var expiresDate = new Date();

expiresDate.setFullYear(expiresDate.getFullYear() + 1);
document.cookie = encodeURI("setCookie="
+ document.colorchoice.selection.value)+ "; expires = "+ expiresDate.toUTCString();

alert("Your background color has been saved for your next visit to this page");


}


function colorit() {
var Color = parseInt(document.colorchoice.selection.selectedIndex)
changeColor(Color)

}



var colorlist = new Array("red","blue","yellow")
function changeColor(color) {
document.bgColor = colorlist[color]
document.colorchoice.value = colorlist[color]
}



</script>
</head>
<body>
<form name="colorchoice">
<p> <h2>Click button to pick color.</h2></p>
<SELECT NAME="selection" value="selectedIndex">
<OPTION>Red
<OPTION>Blue
<OPTION>Yellow
</SELECT>
<INPUT TYPE="button" NAME="Change_Color" VALUE="Change Color" onClick="colorit(); setCookie();">


</form>


</body>
</html>

A1ien51
05-05-2004, 07:32 PM
look at this:

http://javascript.internet.com/cookies/favorite-bg.html

Eric

JPM
05-05-2004, 07:36 PM
expiresDate.toUTCString();

Shouldn't that be ....'toGMTString()' ?

normpeterson
05-05-2004, 07:57 PM
Thanks A1ien51 and JPM! I changed the code to GMTS, but still no luck. I saw the code from the link, but would like to keep my code if possible and use the button to set the cookie. I was looking at the coded link, but could not figure out how to change it for my code to work. Sorry, It gets pretty confusing and I am fairly new to javascript. Isn't there a function I can add to keep these cookies, and just change it to the function and names I am using? This seems like a basic script I wrote and I am surprised this seems so difficult to just keep a cookie. Thanks again for the help guys! Norman.

liorean
05-06-2004, 04:42 AM
[object Date].toUTCString (http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/date.html#1195045)

It should be UTC.




Hmm, have you checked the JavaScript Console? You're trying to set the value of the form itself, which is plainly wrong, in the following passage

function changeColor(color) {
document.bgColor = colorlist
[color=red]document.colorchoice.value = colorlist[color]
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum