CarolinaN
06-10-2005, 11:29 PM
I appreciate everyone that has looked at my posts. I have one last script to understand from the book that I am studying, and then I am off to the book store to get a better book. So if you will please look at this code and let me know what I am doing wrong I will be very grateful.
I simply want to learn how to create a cookie that is assigned the user’s favorite color. And change the background color of the document to that color. So far I think I may be storing it in the cookie correctly, and I have a function that sets the background color, but I don’t think I am recalling it properly when I reopen the web page.
<html>
<head>
<title>Set BGcolor with cookie</title>
<script language="JavaScript">
var yourcolor=""
function makeCookie(form){
var when = new Date();
when.setTime(when.getTime() + 24 * 60 * 60 * 1000);
// 24 hours from now
when.setFullYear(when.getFullYear() + 1);
// One year from now
ycolor=document.form1.yourcolor.value;
document.cookie=escape("BGcolor")+"="+escape(ycolor)+
";expires="+when.toGMTString();
alert(document.cookie);
document.bgColor=document.form1.yourcolor.value;
}
function setColor(form) {
ycolor=document.form1.yourcolor.value;
var position=document.cookie.indexOf("BGcolor=");
if(document.cookie == ""){
alert("No cookies totay");
return false;
}
else {
if ( position != -1){
var begin = position + 5;
var end=document.cookie.indexOf(";", begin);
if(end == -1)
{ end=document.cookie.length;
}
ycolor= unescape(document.cookie.substring(begin, end));
document.bgColor=document.form1.yourcolor.value;
}}
}
</script>
</head>
<body onLoad="setColor()">
<h2>Background Color</h2>
<form name="form1">
<p>What color would you like the background to be? <br>
<input type="text" name="yourcolor" size="30">
</p>
<p><input type="button" value="Make cookie" onclick="makeCookie();">
</p>
</form></body></html>
CarolinaN
I simply want to learn how to create a cookie that is assigned the user’s favorite color. And change the background color of the document to that color. So far I think I may be storing it in the cookie correctly, and I have a function that sets the background color, but I don’t think I am recalling it properly when I reopen the web page.
<html>
<head>
<title>Set BGcolor with cookie</title>
<script language="JavaScript">
var yourcolor=""
function makeCookie(form){
var when = new Date();
when.setTime(when.getTime() + 24 * 60 * 60 * 1000);
// 24 hours from now
when.setFullYear(when.getFullYear() + 1);
// One year from now
ycolor=document.form1.yourcolor.value;
document.cookie=escape("BGcolor")+"="+escape(ycolor)+
";expires="+when.toGMTString();
alert(document.cookie);
document.bgColor=document.form1.yourcolor.value;
}
function setColor(form) {
ycolor=document.form1.yourcolor.value;
var position=document.cookie.indexOf("BGcolor=");
if(document.cookie == ""){
alert("No cookies totay");
return false;
}
else {
if ( position != -1){
var begin = position + 5;
var end=document.cookie.indexOf(";", begin);
if(end == -1)
{ end=document.cookie.length;
}
ycolor= unescape(document.cookie.substring(begin, end));
document.bgColor=document.form1.yourcolor.value;
}}
}
</script>
</head>
<body onLoad="setColor()">
<h2>Background Color</h2>
<form name="form1">
<p>What color would you like the background to be? <br>
<input type="text" name="yourcolor" size="30">
</p>
<p><input type="button" value="Make cookie" onclick="makeCookie();">
</p>
</form></body></html>
CarolinaN