Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Moose

Moose Avatar

****
Senior Member

449


August 2005
To my knowledge, there is currently no built in function or method to count the number of cookies a user has on a domain. So I have devised this.

function countCookies(){
var totalcookies = 0;
var copycookie = document.cookie;
while(copycookie.match(/(\w+=.+?(;|$))/)){
totalcookies++;
copycookie = copycookie.replace(RegExp.$1,'');
}
return totalcookies;
}

It is as simple as:

<script type="text/javascript">
<!--
function countCookies(){
var totalcookies = 0;
var copycookie = document.cookie;
while(copycookie.match(/(\w+=.+?(;|$))/)){
totalcookies++;
copycookie = copycookie.replace(RegExp.$1,'');
}
return totalcookies;
}
alert(countCookies());
//-->
</script>

Greg says:
Coding music...
Greg says:
Hmm...
Greg says:
I wouldn't rule it out. :P
Chris 3.0 [New features? Yes.] says:
:P
Greg says:
If you think about machine code it's a bunch of 1s and 0s
Chris 3.0 [New features? Yes.] says:
Anything is possible.
Greg says:
Yeah try to code Metallica
Chris 3.0 [New features? Yes.] says:
:P Yeah, I'll get right on that... right after I learn to fly.

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
Another maybe...

function countCookies() {
     return document.cookie.split(";").length;
}

Mithras

Mithras Avatar

****
Studio Member

600


July 2006
Moose, your code is one cookie short, and Aaron, yours works.


Support Rob Scuderi, the #1 Penguins Defender!

lucifer said:
"Behold: me! I have authority in this building."

Moose

Moose Avatar

****
Senior Member

449


August 2005
mithras said:
Moose, your code is one cookie short, and Aaron, yours works.

Are you sure it's one cookie short? It's always worked for me. Sadly, though it looks like I have a bad habit of over complicating codes. :(
Greg says:
Coding music...
Greg says:
Hmm...
Greg says:
I wouldn't rule it out. :P
Chris 3.0 [New features? Yes.] says:
:P
Greg says:
If you think about machine code it's a bunch of 1s and 0s
Chris 3.0 [New features? Yes.] says:
Anything is possible.
Greg says:
Yeah try to code Metallica
Chris 3.0 [New features? Yes.] says:
:P Yeah, I'll get right on that... right after I learn to fly.

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Moose, may be it's because you don't check if ends with ; in the cookie? *shrugs*

Moose

Moose Avatar

****
Senior Member

449


August 2005
cddude229 said:
Moose, may be it's because you don't check if ends with ; in the cookie? *shrugs*

Well I am pretty sure cookies aren't allowed to have ; in them. I am not having the same problem as Mithras though my return script returns the correct amount of cookies to me and I have counted them in cookie information.
Greg says:
Coding music...
Greg says:
Hmm...
Greg says:
I wouldn't rule it out. :P
Chris 3.0 [New features? Yes.] says:
:P
Greg says:
If you think about machine code it's a bunch of 1s and 0s
Chris 3.0 [New features? Yes.] says:
Anything is possible.
Greg says:
Yeah try to code Metallica
Chris 3.0 [New features? Yes.] says:
:P Yeah, I'll get right on that... right after I learn to fly.

Mithras

Mithras Avatar

****
Studio Member

600


July 2006
I just compared your result to the one that the web developer addon gave me. :-/


Support Rob Scuderi, the #1 Penguins Defender!

lucifer said:
"Behold: me! I have authority in this building."

newBookmarkLockedFalling