...

View Full Version : Cookie problems



mcfilmmakers
08-29-2004, 07:47 AM
The following is the script and selection menu I am using to let a user choose a background for a table:

<script>
function changeTableBg(selectObject){
var imgName=selectObject[selectObject.selectedIndex].value;
document.getElementById("myTable").setAttribute("background",imgName)
}
</script>


and the actual selection menu:

<select onChange="changeTableBg(this)" >
<option value="bg2.jpg">Select a color below</option>
<option value="bg2.jpg">New Blue</option>
<option value="bg1.jpg">Feeling Green?</option>
</select>


What i need help with is the code to set and retrieve a cookie that will remember what background the user chose for future visits. Everytime I attempt making a cookie, all I end up with is the cookie remembering the option but not applying the effect (ex: the cookie remembers the "Feelign Green?" text but doesn't actually set the "bg1.jpg" image as the background).

Can someone help me?

dumpfi
08-29-2004, 09:36 AM
function changeTableBg(selectObject){
var imgName=selectObject.options[selectObject.selectedIndex].value;
document.getElementById("myTable").setAttribute("background",imgName)
document.cookie = 'background=' + imgName;
}
window.onload = select_bg;
function select_bg() {
if(document.cookie) {
var img = document.cookie.split('=');
document.getElementById("myTable").setAttribute("background", img[1]);
}
}

dumpfi

Willy Duitt
08-29-2004, 10:09 AM
Heh Dumpfi;

I thought I should point out that without setting and expiry date for the cookie, the cookie will be session only and will not be available for future visits....

Here is my offering... Depending upon your host... You may need to uncomment the domain and path line...



<script type="text/javascript">
<!--//
function setCookie(imgName){
var date = new Date();
date.setTime(date.getTime() + 365*24*60*60*1000);
var expString = '; expires = '+date.toGMTString();
// expString+= '; domain = '+location.host+'; path=\/;';
document.cookie = 'imgName='+imgName+expString;
document.getElementById("myTable").setAttribute("background",imgName)
}

function chkCookie(){
if(/^imgName=(\w+)$/.test(document.cookie)){
document.getElementById("myTable").setAttribute("background",RegExp.$1)
} else return;
}
//-->
</script>
</head>

<body onload="chkCookie()">
<form>
<select onChange="setCookie(this.value)" >
<option value="bg2.jpg">Select a color below</option>
<option value="bg2.jpg">New Blue</option>
<option value="bg1.jpg">Feeling Green?</option>
</select>
</form>


.....Willy

mcfilmmakers
08-29-2004, 06:00 PM
I greatly appreciate your help!

mcfilmmakers
08-29-2004, 06:16 PM
I just tested the code and it has the same problem as my attempt, it doesn't apply the background. All it remembers is the option chosen but doesn't actually apply the background.

Willy Duitt
08-29-2004, 06:18 PM
Then you are doing something wrong...
You will need to provide a link or at least some code....

mcfilmmakers
08-29-2004, 06:33 PM
Here is the basic coding for my website. Perhaps it might give you a better way of helping me figure this out.

<body>
<script>
function changeTableBg(selectObject){
var imgName=selectObject[selectObject.selectedIndex].value;
document.getElementById("myTable").setAttribute("background",imgName)
}
</script>

<br />
<br />

<div align="center">
<center><table BORDER=0 CELLSPACING=0 CELLPADDING=0 COLS=1 WIDTH="760" >
<tr VALIGN=TOP>
<td VALIGN=TOP>
<p align="center">
<br>&nbsp;
<select onChange="changeTableBg(this)" >
<option value="bg2.jpg">Select a color below</option>
<option value="bg2.jpg">New Blue</option>
<option value="bg1.jpg">Feeling Green?</option>
</select>

</p>
</td>
</tr>

</table></center>
</div>
<div align="center">
<center>

<table background="" width="760" id="myTable" cellspacing="0" cellpadding="12">
<tr>
<td width="50%" valign="top"><font size="-1" face="Verdana">Column 1
with lots of text</font>
</td>
<td valign="top" width="50%">
column 2 with more text</td>
</tr>
</table>
</center>
</div>
</body>


The thing that really bothers me is that the cookie seems to work, it just doesn't apply or activate the background colors. If it can remember the choice, why can't it set the background?

Willy Duitt
08-29-2004, 07:05 PM
What's that??

Where is the cookie script??
How can someone tell what you are doing wrong if you do not show us the script you tried to implement and which you say doesn't work??

.....Willy

Edit: BTW:....
Please read the Posting Guidelines and use
.....CODE HERE..... tags when posting code....

mcfilmmakers
08-29-2004, 09:29 PM
Here's the code for the entire web page (the basics) with the cookie code in place.



<!-- HERE'S THE COOKIE CODING (IN THE HEADER)--!>
<script type="text/javascript">
<!--//
function setCookie(imgName){
var date = new Date();
date.setTime(date.getTime() + 365*24*60*60*1000);
var expString = '; expires = '+date.toGMTString();
expString+= '; domain = '+location.host+'; path=\/;';
document.cookie = 'imgName='+imgName+expString;
document.getElementById("myTable").setAttribute("background",imgName)
}

function chkCookie(){
if(/^imgName=(\w+)$/.test(document.cookie)){
document.getElementById("myTable").setAttribute("background",RegExp.$1)
} else return;
}
//-->
</script>
</head>

<!-- THE CODING TO CHECK FOR A COOKIE --!>


<body onload="chkCookie()" bgcolor="#000000" text="#FFFFFF">

<br />
<br />

<div align="center">
<center><table BORDER=0 CELLSPACING=0 CELLPADDING=0 COLS=1 WIDTH="760" >
<tr VALIGN=TOP>
<td VALIGN=TOP>
<p align="center">
<br><em>The logo is here</em>

<!--THE FORM CODE IS HERE, YOU CAN SEE THAT THE FORM IS IN A TABLE ON TOP OF ANOTHER TABLE (WHICH IS THE ONE THAT CHANGES BACKGROUND)--!>

<form><select onChange="setCookie(this.value)">
<option value="bg2.jpg">Select a color below</option>
<option value="bg2.jpg">New Blue</option>
<option value="bg1.jpg">Feeling Green?</option>
</select>
</form>

</td>
</tr>

</table></center>
</div>
<div align="center">
<center>

<!-- THIS IS WHERE THE BACKGROUND IS SUPPOSED TO CHANGE. THE CODE WORKS BUT THE COOKIE FAILS TO ACTUALLY SET THE REMEMBERED COLOR --!>


<table background="" width="760" id="myTable" cellspacing="0" cellpadding="12">
<tr>
<td width="50%" valign="top"><font size="-1" face="Verdana">This is the
basic outline of the site</font>
</td>
<td valign="top" width="50%">
The cookie code is in place but doesn't work!</td>
</tr>
</table>
</center>
</div>
</body>


I hope this helps a little more. I tried placing the script in many different places but always end up with the same result. I know we're close, but I must be overlooking something.

Willy Duitt
08-29-2004, 11:51 PM
Try changing the regular expression to: /^imgName=(\w+\.\w+)$/ or if your image paths also include sub directories... Try using Dumpfi's split method rather than a regular expression....

.....Willy

mcfilmmakers
08-30-2004, 03:59 AM
that did the trick, thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum