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


Quick Links:


newBookmarkLockedFalling

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
These are a few open source cookie snippets I just made. Use 'em how you want. :P

- setCookie
- getCookie (Uses RegExp to get the value.)
- getCookie2 (Uses string methods to get the value.)
- deleteCookie


function setCookie(_name,_value){
document.cookie = _name+"="+escape(_value)+";expires="+(new Date()).toGMTString().replace(/\d{4}/,"2050");
}

function getCookie(_name){
if(document.cookie.match(new RegExp(_name+"=(.+?)($|;)","gi")))
return unescape(RegExp.$1);
return null;
}

function getCookie2(_name){
if(document.cookie.indexOf(_name) == -1)
return null;
var _c = document.cookie.substring(document.cookie.indexOf(_name));
return unescape(_c.substring(_c.indexOf("=")+1,(_c.indexOf(";") == -1?_c.length:_c.indexOf(";"))));
}

function deleteCookie(_name){
document.cookie = _name+"=;expires="+(new Date()).toGMTString().replace(/\d{4}/,"2004");
}



Last Edit: Jul 27, 2006 15:43:03 GMT by Chris

Virtuoso

Virtuoso Avatar

****
Senior Member

271


May 2006
Ah, nice. :)

Jay

Jay Avatar
Le geai revient!

*
New Member

9


July 2006
Not too shabby, but lets say the cookie string ends with no ';'. How do you plan to grab it's value? :P

To make it interesting and also stray away from the RegEx method, I revised an addition to your string method. It was fun. :)


<script type="text/javascript">
<!--

function get_cookie(name) {
var beginning, mid, end;
var _dc = document.cookie;
    if (_dc.indexOf(name) != -1) {
        while (0 < _dc.length) {
            mid = _dc.substring(_dc.indexOf(name));
            beginning = mid.indexOf('=');
                if (mid.indexOf(';') == -1) { // checking the end of the string for a semi-colon
                    end = document.cookie.length; // not found, use the length of the string
                }
                else {
                    end = mid.indexOf(';'); // if found, use the index of the semi-colon
                }
            beginning = parseInt(beginning) + 1;
            return unescape(mid.substring(beginning, end));
        }
    }
}

//-->
</script>



Jay


Last Edit: Jul 26, 2006 21:44:32 GMT by Jay

Virtuoso

Virtuoso Avatar

****
Senior Member

271


May 2006
...I really do need a JS book.

Nate

Nate Avatar

**
Official Member

85


July 2007
function getCookie(_name){
if(document.cookie.match(new RegExp(_name+"='(.+?)'($|;)","gi")))
return unescape(RegExp.$1);
return null;
}
You need the quotes if you don't want the string to come in quotes.

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
You guys misunderstand a few things.


1. indexOf returns -1 if no value is found. if -1 is a parameter in substring, it goes to the end.
2. What quotes Nate? =/ I use "escape" to avoid conflicts with quotes and ";" when storing values.....

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
I've always used split().

document.cookie.split("COOKIE_NAME=")[1].split(";")[0];

If no semi-colon exists it will have nothing to split so it will work with or without ";" at the end of the string.

Edit: Jay, it helps to write } else { on one line. Just another little parsing bug with some browsers. ;)






Last Edit: Jul 27, 2006 3:06:48 GMT by Aaron

Jay

Jay Avatar
Le geai revient!

*
New Member

9


July 2006
cddude229 said:
You guys misunderstand a few things.

1. indexOf returns -1 if no value is found. if -1 is a parameter in substring, it goes to the end.

No, that's incorrect. It doesn't go to the end of the string.

function getCookie2(_name){
if(document.cookie.indexOf(_name) == -1)
return null;
var _c = document.cookie.substring(document.cookie.indexOf(_name));
return unescape(_c.substring(_c.indexOf("=")+1,_c.indexOf(";")));
}



Using the above code will only grab a cookie's value if it ends with a semi-colon. It will not grab the last cookie in the string because your telling the substring to stop at the index where the next semi-colon is.

On a vanilla test board (with only the user and pass in the cookie string), using your cookie function, try to grab the pass. You will see that it returns 'pass='; instead of it's value.

I have no idea what Nate is talking about. ???

Jay


Last Edit: Jul 27, 2006 11:44:44 GMT by Jay

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Strange.... worked for me when I used it. I use the RegExp method anyway, so no problem for me. :P

I went ahead and updated my code, so it works now. But thanks for pointing that out.

Jay

Jay Avatar
Le geai revient!

*
New Member

9


July 2006
Hehe, no problems dude. :)

They are great functions. Anyone working with cookies should definitely use them as a quick way to access, modify, or otherwise delete cookies. :P

Jay


Last Edit: Jul 27, 2006 17:44:38 GMT by Jay

Mithras

Mithras Avatar

****
Studio Member

600


July 2006
Chris, thank you so much for making these.


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
It's pretty good but maybe add two more functions, one for session cookies and another for people who want a specific experation date. Or you could modify setCookie so it's exactly like the PHP one. :P
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
They're open source, they can modify them if they want. :P

newBookmarkLockedFalling