tags:

views:

2370

answers:

5

I need to get all the cookies stored in my browser using javascript. How can it be done?

+17  A: 
LeopardSkinPillBoxHat
+1 on general principles!
Adam Liss
A pleasure to see a (good) joke not downvoted... :-)
PhiLho
Sadly, SO intent is to provide help to people that ask questions, not hours of amusement. Nevertheless, it's a good joke. Wouldn've been even better if you actually put the answer below.
Franci Penov
I can't resist upvoting something that actually made me laugh out loud.
eyelidlessness
can't resist either, +1 LOL
seanb
+6  A: 

To retrieve all cookies for the current document open in the browser, you again use the document.cookie property.

Codeslayer
+4  A: 

You cannot. By design, for security purpose, you can access only the cookies set by your site. StackOverflow can't see the cookies set by UserVoice nor those set by Amazon.

PhiLho
+2  A: 
  1. You can't see cookies for other sites.
  2. You can't see http-only cookies.
  3. All the cookies you can see are in the document.cookie property, which contains a semicolon separated list of name=value pairs.
Franci Penov
+7  A: 

You can only access cookies for a specific site. Using document.cookie you will get a list of escaped key=value pairs seperated by a semicolon.

secret=do%20not%20tell%you;last_visit=1225445171794

To simplify the access, you have to parse the string and unescape all entries:

var getCookies = function(){
  var pairs = document.cookie.split(";");
  var cookies = {};
  for (var i=0; i<pairs.length; i++){
    var pair = pairs[i].split("=");
    cookies[pair[0]] = unescape(pair[1]);
  }
  return cookies;
}

So you might later write:

var myCookies = getCookies();
alert(myCookies.secret); // "do not tell you"
aemkei