470,618 Members | 1,698 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,618 developers. It's quick & easy.

cookie example - where's the bug?

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?
Jul 23 '05 #1
4 1363
In article <fd**************************@posting.google.com >,
as*******@hotmail.com enlightened us with...
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.
--
--
~kaeli~
Why do they sterilize the needles for lethal injections?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2
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!

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

"Asad Khan" <as*******@hotmail.com> wrote in message
news:40*********************@news.frii.net...

and setCookie is called on body onload. Same problem with alert. Says
undefined!
Of course it does - if the block containing the alert() is executed,
cookie_counter has been assigned nothing.

The statement document.cookie=0 will save nothing.

The minimum format for a cookie that survives the current session is;
cookiename=value;expires=gmtstring
if (document.cookie == "")


The document.cookie property is a string that can be comprised of many cookies,
not all necessarily created by the current document. Consequently, to read the
value of a particular cookie, you must search for its offset in the string.

var cOffset, cName="myCookie=", cValue=null;

if( document.cookie && (cOffset=document.cookie.indexOf( cName )) >-1 ) //get
offset of cookie's name
{
cOffset+=cName.length; //bump to the start of the value

cValue=document.cookie.substring(cOffset, document.cookie.length); //read from
start of value

if( !isNaN( cValue=parseInt( cValue ) ) )
cValue+=1 //or whatever you want to do with it
}
For debugging purposes, if you want to see the contents of document.cookie, type
in the address bar:

javascript:alert(document.cookie)

--
Stephen Chalmers


Jul 23 '05 #4
kaeli wrote:
In article <40*********************@news.frii.net>,
as*******@hotmail.com enlightened us with...
No, that's not it. Yes I do declare it outside.
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
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Stijn Goris | last post: by
3 posts views Thread by Daan | last post: by
2 posts views Thread by marshalli | last post: by
5 posts views Thread by | last post: by
15 posts views Thread by Oleg Leikin | last post: by
1 post views Thread by BJŲrn Lindqvist | last post: by
6 posts views Thread by kelvlam | last post: by
29 posts views Thread by Jerim79 | last post: by
16 posts views Thread by Stevo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.