I need to get all the cookies stored in my browser using javascript. How can it be done?
+1 on general principles!
Adam Liss
2008-10-31 05:33:31
A pleasure to see a (good) joke not downvoted... :-)
PhiLho
2008-10-31 05:56:15
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
2008-10-31 06:17:34
I can't resist upvoting something that actually made me laugh out loud.
eyelidlessness
2008-10-31 07:01:19
can't resist either, +1 LOL
seanb
2008-10-31 07:03:49
+6
A:
To retrieve all cookies for the current document open in the browser, you again use the document.cookie
property.
Codeslayer
2008-10-31 05:34:49
+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
2008-10-31 06:00:28
+2
A:
- You can't see cookies for other sites.
- You can't see http-only cookies.
- All the cookies you can see are in the
document.cookie
property, which contains a semicolon separated list of name=value pairs.
Franci Penov
2008-10-31 06:12:39
+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
2008-10-31 09:28:31