Click to See Complete Forum and Search --> : My cookie has two of the same name


JSchwarz
07-10-2003, 11:39 AM
For some reason, my cookie string contains the same name (with different value) twice. Anyone know why?

I have the following basic functions (from O'Reilly's JavaScript Cookbook) for getting and setting the cookie value (don't read the whole functions, they're basic and provided for reference):
// utility function called by getCookie()
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) {
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
}

// primary function to retrieve cookie by name
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;
}

// store cookie value with optional details as needed
function setCookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");

In my site's legal warning, disclaimer page, I execute the following:
setCookie("ASSIST", "*");

If I view the cookie at this point it's value is "; ASSIST=*" (sans quotes).

On the next page, I have a select-one element with the following onBlur, which basically just sets the cookie value to the value the user selected:
var beenWarned = getCookie("ASSIST");
var SSID = thisForm['SSID'];

if (SSID = SSID.options[SSID.selectedIndex].value) {} else SSID="*";

if (SSID != beenWarned) setCookie("ASSIST", SSID);

After setting the value in SSID, ASSIST is in the cookie string two times, like this: ASSIST=1665ECAE23FE472085256D570074C2E7; ; ASSIST=*

Why is the same value being added twice? Why doesn't it replace the first one? If the user makes a second (or third and so on) choice, it replaces the first value -- but the second (the asterisk) always remains).