5

how can i get all the cookies set by my site using js. I dont want to do say Cookie("username") but loop through all the cookies and get the key=value pairs of my site

aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242
  • You've tagged this PHP and JavaScript. Which language are you using? Neither of them have a built in function called `getCookie`. – Quentin Feb 07 '11 at 09:58

4 Answers4

4
var cookies = document.cookie.split(/;/);
for (var i = 0, len = cookies.length; i < len; i++) {
   var cookie = cookies[i].split(/=/);
   alert("key: " + cookie[0] + ", value: " + cookie[1]);
}
RoToRa
  • 37,635
  • 12
  • 69
  • 105
  • [document.cookie()](https://developer.mozilla.org/en-US/docs/Web/API/document.cookie) is as close as you can get but it doesn't retrieve all the cookies from the site—it merely retrieves cookies from current *document*. – Álvaro González Oct 07 '13 at 10:23
  • That is not true. "Documents" don't have cookies. Cookies are stored per domain, or they can be assigned to a path within that domain, but that has nothing to do with the JavaScript/DOM concept of documents. – RoToRa Oct 07 '13 at 13:28
  • Yet the `cookie` method belongs to the `document` object? I'm not saying your answer is wrong, I'm just stating that the original question ("get all the cookies set by my site") cannot be answered. Running your snippet from e.g. `/` will not list cookies set for `/another-path` (even though the browser cookie manager shows they exist). It's very easy [to test locally](http://pastebin.com/k5UNYhBe). – Álvaro González Oct 07 '13 at 14:40
  • The `cookie` *property* isn't very well designed. But since there is no `domain` object, the `document` makes more sense than the `window` object, which would be the only other choice. I think the path can be ignored in this case. – RoToRa Oct 08 '13 at 14:45
  • I've posted test code that verifies my assertion (that `document.cookie` takes path into account). Can you please post test code that verifies yours? – Álvaro González Oct 08 '13 at 14:49
2

You can use the getCookie from my answer to Javascript getCookie functions and split it into a getCookies and getCookie function where the getCookies function simply returns cookies instead of cookies[name]. And for the getCookie function just take the return value of getCookies and use [name] on it.


Update    Ok, I simply added the functions according to the description above. :)

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844
0

Please read about reading/writing cookies in JavaScript.

http://www.quirksmode.org/js/cookies.html

You are probably looking for a solution like this: Get all cookies with Javascript

The following function loads all the cookie items into an associative array with the cookie name as the index and the cookie value as the value:

function get_cookies_array() {

    var cookies = { };

    if (document.cookie && document.cookie != '') {
        var split = document.cookie.split(';');
        for (var i = 0; i < split.length; i++) {
            var name_value = split[i].split("=");
            name_value[0] = name_value[0].replace(/^ /, '');
            cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
        }
    }

    return cookies;

}

After that, you can get the cookies and write them out into the document like this:

var cookies = get_cookies_array();
for(var name in cookies) {
  document.write( name + " : " + cookies[name] + "<br />" );
}
Arseny
  • 5,159
  • 4
  • 21
  • 24
0
<html>
<head>
<script type="text/javascript">
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
  {
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function checkCookie()
{
var username=getCookie("username");
if (username!=null && username!="")
  {
  alert("Welcome again " + username);
  }
else 
  {
  username=prompt("Please enter your name:","");
  if (username!=null && username!="")
    {
    setCookie("username",username,365);
    }
  }
}
</script>
</head>
<body onload="checkCookie()">
</body>
</html>

copy pasted from : http://www.w3schools.com/JS/js_cookies.asp

Shrinath
  • 7,888
  • 13
  • 48
  • 85