Function to keep a cookie for page re-load.

  Post new topicReply to topicPrintable Version
<< View previous topic View next topic >>
Share: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
#1   Function to keep a cookie for page re-load.
normpeterson2
CZ Newbie
normpeterson2 has been a member for over 20 year's 20 Year Member
Status: Offline
Joined: May 05, 2004
0.00 posts per day
Posts: 2
Points: 136
   
Hello all! I have a script that changes the color of a page. Problem is that the cookie does not keep the choice when you reload or go back to the page. I have tried some onLoad functions for the body tag with some code that I have tried to modify, but no luck. Is there something I am missing in my setCookie function, or do I need another function besides that? Thanks for the 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>
<SELECT NAME="selection">
<OPTION>Red
<OPTION>Blue
<OPTION>Yellow
</SELECT>
<INPUT TYPE="button" NAME="Change_Color" VALUE="Change Color" onClick="colorit(); setCookie();">
</form>
</body>
</html>



Back to top Reply with quote
#2   re: Function to keep a cookie for page re-load.
Telli
Site Admin
Telli has been a member for over 20 year's 20 Year Member
Occupation: Self Employed
Age: 46
Gender: Male
Fav. Sports Team: Detroit Red Wings
Website:
Status: Offline
Joined: May 26, 2003
1.06 posts per day
Posts: 8089
Points: 494,430
   

<!-- TWO STEPS TO INSTALL COOKIE - FAVORITE BACKGROUND COLOR:

   1.  Paste the designated coding into the HEAD of your HTML document
   2.  Put the last script into the BODY of your HTML document  -->

<!-- STEP ONE: Copy this code into the HEAD of your HTML document  -->
       
<HEAD>

<SCRIPT LANGUAGE = "JavaScript">
<!-- Original:  Mattias Sjoberg -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function color(){
var favColor = GetCookie('color');
if (favColor == null) {
favColor = prompt("What is your favorite background color?");
SetCookie('color', favColor, exp);
}
document.bgColor=favColor;
return favColor;
}
function set(){
favColor = prompt("What is your favorite background color?");
SetCookie ('color', favColor, exp);
}
function getCookieVal (offset) { 
var endstr = document.cookie.indexOf (";", offset); 
if (endstr == -1)   
endstr = document.cookie.length; 
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) { 
var arg = name + "="; 
var alen = arg.length; 
var clen = document.cookie.length; 
var i = 0; 
while (i < clen) {   
var j = i + alen;   
if (document.cookie.substring(i, j) == arg)     
return getCookieVal (j);   
i = document.cookie.indexOf(" ", i) + 1;   
if (i == 0) break;   

return null;
}
function SetCookie (name, value) { 
var argv = SetCookie.arguments; 
var argc = SetCookie.arguments.length; 
var expires = (argc > 2) ? argv[2] : null; 
var path = (argc > 3) ? argv[3] : null; 
var domain = (argc > 4) ? argv[4] : null; 
var secure = (argc > 5) ? argv[5] : false; 
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) + 
((domain == null) ? "" : ("; domain=" + domain)) +   
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) { 
var exp = new Date(); 
exp.setTime (exp.getTime() - 1); 
var cval = GetCookie (name); 
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
// End -->
</SCRIPT>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>
<a href="JavaScript:set()">Change background color</a>
</textarea><br></td></tr>
</table>
</form>




_________________
The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee. Ezekiel 25:17
Back to top Reply with quote
#3   re: Function to keep a cookie for page re-load.
normpeterson2
CZ Newbie
normpeterson2 has been a member for over 20 year's 20 Year Member
Status: Offline
Joined: May 05, 2004
0.00 posts per day
Posts: 2
Points: 136
   
Hi Telli! Thanks for the help! Is there any way though to keep my code and make the cookie stay? I need to use a button to set the cookie to remember the users choice when they return to the page. Thanks again!



Back to top Reply with quote
Display posts from previous:      
Add To: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
<< View previous topic View next topic >>
Post new topicReply to topic

Jump to 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum