|
These are a few open source cookie snippets I just made. Use 'em how you want.  - 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
|
|
|
|
|
Ah, nice.
|
|
|
|
|
Not too shabby, but lets say the cookie string ends with no ';'. How do you plan to grab it's value?  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
|
|
|
|
...I really do need a JS book.
|
|
|
|
|
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.
|
|
|
|
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.....
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
Strange.... worked for me when I used it. I use the RegExp method anyway, so no problem for me.  I went ahead and updated my code, so it works now. But thanks for pointing that out.
|
|
|
|
|
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.  Jay
Last Edit: Jul 27, 2006 17:44:38 GMT by Jay
|
|
|
|
|
|
They're open source, they can modify them if they want.
|
|
|
|