views:

629

answers:

1

I am observing, on both Firefox and IE, that if I have a cookie 'x' on the domain a.b.c.com, and also have a cookie with the same name 'x' on domain a.b.com, then when I look at the value of document.cookie on the a.b.c.com domain, it shows both cookies. I would like to see just the cookie from the a.b.c.com domain, and not the one from the other domain. (I'm assuming this occurs because one domain is the same as the other one, with an additional segment on the hostname.) Is there a way to do this?

I don't have control over the contents of the cookie, and I don't see anything obvious in those contents that distinguishes one domain from the other.

+1  A: 

You don't have access to the domain of the cookie in Javascript.

"When [the cookie] attribute is read, all cookies are returned as a single string, with each cookie's name-value pair concatenated into a list of name-value pairs, each list item being separated by a ';' (semicolon)."
W3C

When you read a cookie, you only have access to the name/value pairs, and cannot determine any other information about it. If you require things such as when it was set, what domains it was set for, or anything else, you have to store it inside the cookie value.

Since you cannot set the cookies, you need another method to do what you're attempting.

Ian Elliott
Yeah, that's what the IE team told me too. Major bummer.
Bruce