By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,584 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,584 IT Pros & Developers. It's quick & easy.

Finding cookie name?

P: n/a


When I reference document.cookie, there is a long string of key=value;
pairs listed. I may have 100 hundred cookies on my hard drive.
However, most only have one key=value pair. Does the document.cookie
variable combine all cookie key=value pairs?

All of the examples I've seen discuss referencing a specific cookie. I
don't see how this is done.

Cookies are usually named by the domain. If I want to reference a
specific cookie, do I need to search for my key=value pair by parsing
the document.cookie string? If I have several key=value; pairs in one
cookie, must I continue parsing for those specific key=value; pairs?

Judging by most getCookie() functions I've seen, the above seems to be
the case. I can't just call a specific cookie. I must reference
document.cookie and search for my specific key=value pairs. However, I
may be going about it the wrong way. Please clarify.

Thanks,
Brett

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <41*********************@news.newsgroups.ws>, my*******@cygen.com
enlightened us with...
Does the document.cookie
variable combine all cookie key=value pairs?

Yes, it does.
All of the examples I've seen discuss referencing a specific cookie. I
don't see how this is done.

There are a couple ways to do it.
Cookies are usually named by the domain.
Not always.
If I want to reference a
specific cookie, do I need to search for my key=value pair by parsing
the document.cookie string? If I have several key=value; pairs in one
cookie, must I continue parsing for those specific key=value; pairs?


Yes.

Here's my cookie.js file if it helps you. You can get your cookie into a
string by using its name this way:
var myCk = getCookie("myCookieName");

Watch for word-wrapping.
Note that I don't bother with setting a lot of the attributes, such as
domain, as I don't need to. You could modify this to do that.

--------------------------------------------------------------

/* jsCookies.js
This file contains cookie functions. */
/* File Functions:
1. setCookie - writes cookie
2. getCookie - gets value of cookie
3. removeCookie - deletes a cookie
4. detectCookies - checks if cookies are enabled
*/

function setCookie(cookieName, cookieValue, expireDate)
{
/* Pass in three strings - the name of the cookie, the value, and the
expire date.
Pass in a "" empty string for expireDate to set a session cookie (no
expires date).
Pass in any other date for expire as a number of days to be added to
today's date. */

if (expireDate == "")
{
expires = "";
}
else
{
expires = new Date();
expires.setDate(expires.getDate() + expireDate);
expires = expires.toGMTString();
}
document.cookie = cookieName+"="+cookieValue+";expires="+expires;
}

function removeCookie (cookieName)
{
/* Pass in the name of the cookie as a string and it will be removed. */
expires = Now();
document.cookie = cookieName+"= ;expires="+expires.toGMTString();
}

function getCookie (cookieName)
{
cookieValue = ""
if (document.cookie.indexOf(cookieName) == -1)
{
// there is no cookie by this name for this user
return cookieValue;
}
else
{
// get the beginning index of the cookie by looking for the cookie name
cookieStart = document.cookie.indexOf(cookieName);
// get the beginning index of the cookie value by looking for the equal
sign after the name
cookieValStart = (document.cookie.indexOf("=", cookieStart) + 1);
// get the end index of the cookie value by looking for the semi-colon
after the value
cookieValEnd = document.cookie.indexOf(";", cookieStart);
// if no semi-colon, then use the whole length
if (cookieValEnd == -1)
{
cookieValEnd = document.cookie.length
}
// use substring to get the text between the two indices and that is
the value of the cookie
cookieValue = document.cookie.substring(cookieValStart, cookieValEnd);
return cookieValue;
}
}

function detectCookies()
{
/* function returns true if cookies are enables, false if not */
setCookie("test", "test", "");
tmp = getCookie("test")
if (tmp != "test")
{
return false;
}
else
{
return true;
}
}

/* EOF */
-----------------------------------------------------------------------

--
--
~kaeli~
Never mess up an apology with an excuse.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2

P: n/a
brettr wrote:
When I reference document.cookie, there is a long string of key=value;
pairs listed. I may have 100 hundred cookies on my hard drive.
You maybe want to remove some because it is likely that there are spies
among them.
However, most only have one key=value pair. Does the document.cookie
variable combine all cookie key=value pairs?
All for one resource, when reading the property, yes.
All of the examples I've seen discuss referencing a specific cookie.
URL?
I don't see how this is done.
You could split the string value and then search for the respective key.
It then helps to split the key/value pairs again on the "=" and then
search for the key. Examples have been posted here before.
Cookies are usually named by the domain. If I want to reference a
specific cookie, do I need to search for my key=value pair by parsing
the document.cookie string? If I have several key=value; pairs in one
cookie, must I continue parsing for those specific key=value; pairs?
Yes, but parsing the whole string may not be the most efficient/viable method.
Judging by most getCookie() functions I've seen,
URL(s)?
the above seems to be the case. I can't just call a specific cookie.
I must reference document.cookie and search for my specific key=value
pairs.


Yes, indeed.
PointedEars
Jul 23 '05 #3

P: n/a
kaeli wrote:
<snip>
function getCookie (cookieName)
{
cookieValue = ""
There are quite a lot of global variables in this funciton that really
should be local.
if (document.cookie.indexOf(cookieName) == -1)

<snip>

The FAQ notes page:-

<URL: http://jibbering.com/faq/faq_notes/cookies.html >

- describes an extra safety measure necessitated by the use of content
inserting/re-writing proxies (firewalls, internet security programs and
such like) to prevent scripts from using cookies. They do this by
re-writing - document.cookie - to - document.somethingElse - in the JS
source code. As a result the modified property accessor does not resolve
to a string (it will probably return an undefined value), and an attempt
to call - indexOf - will error as an undefined value will not
type-convert into an object.

Generally code that fails by erroring is undesirable (and potentially
embarrassing for its author if the user is seeing error messages
(proxies usually insert an - onerror - handler to suppress error
messages resulting from their actions)).

The page above proposes using a - typeof - test to verify that the
object returned from the property accessor is a string prior to calling
String object methods on it. A number of alternative safety measures
might be applied (or combined) to achieve a similar effect.

It should be possible to negate the effect of content inserting proxies
by using a bracket notation property accessor with a string value that
did not resemble "cookie":-

document["\x63o\x6Fk\x69e"]

- (or some other escape sequence based munging of "cookie" ) in the hope
that the proxy would fail to recognise and replace that. However, if the
user is running a proxy that is attempting to prevent scripts form
reading and writing cookies then maybe subverting its actions is not a
desirable thing to be doing.

An alternative to using - typeof - to verify that - document.cookie - is
returning a string value might be to assign - new
String(document.cookie) - to a local variable. Subsequent String object
method calls would not then be error-producing, though they would still
not "work" if the property accessor resolved as an undefined value.
However, as a safety measure, explicitly converting the primitive value
returned by the property accessor into a String object would have the
advantageous side-effect of producing more efficient code. The use of a
String object method on a string primitive forces an internal
type-conversions into a String object for each method invocation, so
explicitly doing that up-front in code that uses many String object
methods would avoid the need to have it done implicitly on each method
call:-

function getCookie (cookieName){
var cookieValStart,
cookieValEnd,
cookieValue = "",
ck = new String(document.cookie);
//or - ck = new String(document["\x63o\x6Fk\x69e"]);
if(
((cookieStart = ck.indexOf(cookieName)) != -1)&&
((cookieValStart = (ck.indexOf("=", cookieStart) + 1)) != 0)
){
// get the end index of the cookie value by looking
//for the semi-colon after the value
cookieValEnd = ck.indexOf(";", cookieStart);
// if no semi-colon, then use the whole length
if (cookieValEnd == -1){
cookieValEnd = ck.length;
}
// use substring to get the text between the two
//indices and that is the value of the cookie
cookieValue = ck.substring(cookieValStart, cookieValEnd);
}
return cookieValue;
}

Richard.
Jul 23 '05 #4

P: n/a
Maybe the wording is throwing me off. I see the above code referencing
a cookiename variable being passed into somthing similar to getCookie
(cookieName). If you can't actually reference a specific cookie, why
use something called cookieName?

From what I gather, you can only reference document.cookie and parse it
for your key/value pairs.

Must the expires= be set for each key/value pair? Does this create the
cookie? document.cookie is a variable that grabs key/value pairs and
other information from .txt files and groups them all into one variable.
Ok, from that, how are you defining cookie?

Thanks,
Brett

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #5

P: n/a
On 02 Aug 2004 21:25:09 GMT, brettr <my*******@cygen.com> wrote:
Maybe the wording is throwing me off. I see the above code referencing
a cookiename variable being passed into somthing similar to getCookie
(cookieName). If you can't actually reference a specific cookie, why
use something called cookieName?
You pass the function a string containing the name of the key/value pair
you want to retrieve.
From what I gather, you can only reference document.cookie and parse it
for your key/value pairs.
Which is what the function does. The variable, cookieName, is the key for
the pair.
Must the expires= be set for each key/value pair?
No, it's applies to all the pairs you specify in that assignment. If you
do not specify an expiry value, the cookie becomes a session cookie. If
you do specify a date which is in the past, the included pairs are deleted.
Does this create the cookie?
Just writing the document.cookie property creates the cookie (unless the
expiry date is in the past).
document.cookie is a variable that grabs key/value pairs and
other information from .txt files and groups them all into one variable.
Ok, from that, how are you defining cookie?


I think you'll find it varies. Some define a cookie as a collection of
key/value pairs that exist for a specific path on a specific domain. Some
will define each individual pair as a cookie. I believe the former is the
more accurate definition, but it doesn't really matter.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.