#1
  1. An Ominous Coward
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2002
    Posts
    4,425
    Rep Power
    0

    JavaScript Cookie Behavior Clarification


    I need some clarification on how JavaScript handles cookies via the document.cookie object. I'm having a hard time figuring out what goes on, and the tutorials / books I'm reading aren't helping. When I have a string from document.cookie that looks like this:
    Code:
    name=Clint McLintock; age=20; hair=Messy
    ...I have three separate cookies in one string, correct? Now, if I assign:
    Code:
    document.cookie = "name=Horatio; age = 20; eyes=Missing"
    Now, I should have four separate cookies, with a document.cookie, correct?

    And finally, the only way to eliminate a cookie with JavaScript is to set the expires attribute to the past, correct? Someone please verify or clarify this for me, as I'm having problems with ASP pages writing cookies that seem to not be playing so nice with my JavaScript / perl returned cookies. Once I understand JavaScript's cookie handling, I have the feeling I'll be checking into the ASP cookies that seem to be interfering somehow.

    Thanks in advance!
    Last edited by Ctb; January 2nd, 2005 at 05:28 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    NYC
    Posts
    79
    Rep Power
    13
    Here's my understanding: When you attempt
    document.cookie = "name=Horatio; age = 20; eyes=Missing"
    this is actually invalid since
    (1) you should only set one cookie at a time [this is an inferred assumption on my part] and (2) Cookie values may not include semicolons, commas, or whatespace.

    I think javascript is expecting parameters (expiration date, directory, etc) after that first semicolon, and it won't set any other cookie values.
    (mostly gleaned from Chapter 18 of David Flanagan's Javascript (pub. O'Reiley).

    Csaba
  4. #3
  5. An Ominous Coward
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2002
    Posts
    4,425
    Rep Power
    0
    Well.. okay, my bad :P I'm actually setting one cookie with all three variables globbed together, not three separate cookies like I suggested.

    My big problem is, I set a cookie on one page, that holds some user data, then I set a cookie from another page that acknowledges login, then a stupid ASP page insists on setting a "sessionId" cookie.

    This all seems to work fine, happily setting the new cookies without any complaints, until I try to read a new cookie. It seems like when I set a new cookie (or try to change the value of an existing cookie) with document.cookie, it wipes out the other two that had been set previously. Why? My understanding is that the only way to remove a cookie like this would be to do:
    Code:
    document.cookie="NAME="+escape(somevalue)+"; expires=PAST";
    Yet, setting document.cookie at all (even if I'm trying to use a totally unique cookie name, seemingly winds up wiping out the other cookies. I'm wondering why?
  6. #4
  7. An Ominous Coward
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2002
    Posts
    4,425
    Rep Power
    0
    Ahhh.. I figured it out.

    I had some old cut and paste JavaScript code in the page that was causing me problems. It tested the existence of a cookie, but it didn't take into account the possiblity of more than one cookie (helpful, no?). I just had to edit it so it would look at multiple cookies in the document.cookie string when looking for a name with the indexOf() method.

IMN logo majestic logo threadwatch logo seochat tools logo