473,406 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Finding cookie name?



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
5 3293
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: koen colen | last post by:
Hello group, I hope you guys can help me out, I am modifying a piece of code from Joe Norman, I did found the code here: http://www.intranet2internet.com/public/default.asp?PAGE=sscript&ID=3 ...
4
by: Shannon Jacobs | last post by:
I'm doing some trivial surveys, and I want to know if the same user answers twice. Can't really know that, but at least I thought I could check for the same browser/computer combination by using a...
12
by: chrism | last post by:
Hello, I have a pop-up window that I would like to appear in front of the browser home page when a user opens IE. Problem is, I'd like it to never appear again if the user navigates back to the...
1
by: Mike | last post by:
Hello, I can't find any javascript that reads and writes cookies with keys, so that it is compatible with ASP (I want to read and write cookies from both javascript and ASP) for example in...
4
by: socialism001 | last post by:
I'm trying to store a value in a cookie but its not working. Can anyone see what I might be doing wrong. Thanks, Chris ~~~~~~~~~~~~~~~~~~ <script language="javascript">...
9
by: Marco Krechting | last post by:
Hi All, I have a page with a list of hyperlinks. I want to save information in a cookie about the fact that I entered an hyperlink or not. When I click one of the hyperlinks I want this stored...
1
by: CR1 | last post by:
I found a great cookie script below, but don't know how to make it also pass the values sent to the cookie, to a querystring as well for tracking purposes. Can anyone help? If there was a way to...
6
by: kelvlam | last post by:
Hello all, I'm still a bit new with JavaScript, and I hope the guru here can shed some light for me. It's regarding handling cookie and the case-sensitive nature of JavaScript itself. My...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.