cookie example - where's the bug?

A

Asad

I am trying to get a cookie going on my site. Here is the code first:

function setCookie()
{
if(document.cookie == "")
{
var someDate = new Date("December 31, 2023");
var expiryDate = someDate.toGMTString();
document.cookie = "0";

cookie_counter = document.cookie;
document.write ("Cookie Created: " + cookie_counter);
}
else
{
var temp = parseInt(cookie_counter)
temp = temp + 1
cookie_counter = temp
document.write ("Added 1 to Cookie: " + cookie_counter);
}
}

But this just doesn't work. The first time through, cookie is created
fine and I get a line that reads "Cookie Created: 0". But the second
and following time I keep getting "Added 1 to Cookie: NaN".

I can't figure out what's wrong. I don't even see any cookie file
created in my cookies folder under windows. I even set my IE security
levels to accept all cookies, but still nothing ...

Any ideas?
 
K

kaeli

I am trying to get a cookie going on my site. Here is the code first:

function setCookie()
{
if(document.cookie == "")
{
var someDate = new Date("December 31, 2023");
var expiryDate = someDate.toGMTString();
document.cookie = "0";

cookie_counter = document.cookie;
document.write ("Cookie Created: " + cookie_counter);
}
else
{
var temp = parseInt(cookie_counter)

Where is cookie_counter defined? The only place I see it is in that
"if". Which means it has no value before this else because it's scope is
local to the function.
That is probably your problem, unless you defined it global somewhere
else.
Put an alert(cookie_counter) just before this statement (the var temp
one) and see what's in there.


--
 
A

Asad Khan

No, that's not it. Yes I do declare it outside. Here is my full code
once again:

<script language="javascript">
<!--
var cookie_counter

function setCookie()
{
if (document.cookie == "")
{
var someDate = new Date("December 31, 2023");
var expiryDate = someDate.toGMTString();
document.cookie="0";

cookie_counter = document.cookie;
document.write ("Cookie created!" + cookie_counter);
}
else
{
alert(cookie_counter)
document.write ("Cookie added!" + cookie_counter);
var temp = parseInt(cookie_counter)
temp = temp + 1
cookie_counter = temp
document.write ("Cookie added!" + temp+ "|" +
cookie_counter);
}
}


//-->

</script>

and setCookie is called on body onload. Same problem with alert. Says
undefined!
 
K

kaeli

No, that's not it. Yes I do declare it outside.

But you don't assign it a value.
document.cookie has ALL the cookies. Not just yours.
It is more than possible for it to not be the empty string, as your code
assumes. So your else would be evaluated and the variable would have
nothing in it.

This is my cookie code. Note that a NAME is required for every cookie.

From my jsCookies.js file...
Watch for line breaks with word wrap.

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

/* 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, expireDays)
{
/* Pass in three strings - the name of the cookie, the value, and the
expire days.
Pass in a "" empty string for expireDate to set a session cookie
(no expires date).
Pass in a number of days to be added to today's date as
expireDays. */

if (expireDays == "")
{
expires = "";
}
else
{
expires = new Date();
expires.setDate(expires.getDate() + expireDays);
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;
}
}

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

There's some even better code that sets more cookie properties like
domain, secure, path, etc here.
http://www.acm.uiuc.edu/webmonkeys/javascript/cookies.js

--
 
T

Thomas 'PointedEars' Lahn

kaeli said:
But you don't assign it a value.
True.

document.cookie has ALL the cookies. Not just yours.

No, it has not. Otherwise it would be
possible to read cookies of other sites.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,470
Messages
2,571,807
Members
48,797
Latest member
PeterSimpson
Top