views:

160

answers:

1

I have a cookie I am setting when a user clicks a button on a form with the following "OnClick" handler...

<input type="button" 

       OnClick="

       alert(document.cookie);
       var d;
       d = new Date();
       d.setDate(d.getDate() + 365);

       document.cookie = 'LanguageReference' + '=' + document.forms[0].UserSelectedLanguage.value + ';expires=' + d.toGMTString();
       document.forms[0].submit();" >               

This works great but I've noticed some strange behavior with it when I display the contents of document.cookie and when I have changed the value of "LanguageReference" multiple times. The "LanguageReference" cookie will show up multiple times under document.cookie with different values.

This is what I get...

LanguageReference=en;LanguageReference=fr;... bunch of other cookies ...

This doesn't appear to be affecting retrieval of the cookie oddly enough which is great. I always get back what I want. But I fear there may be an underlying issue I'm not fully understanding which is going to bite me down the road.

+2  A: 

In javascript, cookies are treated as ; and = separated string. You should clear the previous one before add the new cookie. A cookie handler class (or a relevant part of a framework) makes your life much easier.

erenon
Thought that might be the best case. Thanks for the reply.
Rob Segal