I need to get all the cookies stored in my browser using javascript. How can it be done?
-
3nayagi, unfortunately if we do answer your question you would have software that is nothing less than MALWARE. – Jon Limjap Oct 31 '08 at 6:32
-
12@Jon Limjap - that's not quite true. There are legitimate scenarios for being able to enumerate through all cookies in a web browser without being malware. – Franci Penov Oct 31 '08 at 18:47
-
Whoa wait a sec. All of the cookies? Login's from any website? That's malware... – alexy13 Apr 7 '11 at 19:34
-
I have a situation (web apps behind the firewall for a business) where doing this would be handy and quite legitimate. However, the issue exists because we have too many internal domains (mostly through virtual hosts) so one could argue that the solution is to use Single Sign On. – ClubbedAce Jan 14 '14 at 21:12
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]+'').trim()] = unescape(pair[1]);
}
return cookies;
}
So you might later write:
var myCookies = getCookies();
alert(myCookies.secret); // "do not tell you"
-
5this doesnt catch the space after the semicolon ("; ") so keys have a space at the front... use /; ?/ when splitting to remove them? – Carter Cole Dec 20 '11 at 21:43
-
-
should really add .trim() to pair[0] to make sure that there is no leading spaces. – Andrew Killen Jul 15 '16 at 19:53
-
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.
-
2how did facebook doing that...? I always saw facebook shows ads related to what I browsed from google! – KeepMove Apr 8 '15 at 13:02
-
12Actually, Facebook plants its own cookies on every site... using its Like button. I bet Google do the same on the +1 buttons or even on the site stats scripts. – PhiLho Apr 9 '15 at 8:43
-
1thank you..., it's opening my eyes. I think any kind of app related to them will do, including login using social media account app. – KeepMove Apr 10 '15 at 2:29
-
2@SoursopTree - Isn't it AdChoices? Facebook uses AdChoices, which is a Google service that displays content based on your searches through Google. Stackoverflow uses AdChoices too. I may be a bit off on the exact details - but I'd guess that's what it is you're thinking of. – Rik Oct 23 '15 at 8:24
-
1
- 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.
To retrieve all cookies for the current document open in the browser, you again use the document.cookie
property.
Since the title didn't specify that it has to be programmatic I'll assume that it was a genuine debugging/privacy management issue and solution is browser dependent and requires a browser with built in detailed cookie management toll and/or a debugging module or a plug-in/extension. I'm going to list one and ask other people to write up on browsers they know in detail and please be precise with versions.
Chromium, Iron build (SRWare Iron 4.0.280)
The wrench(tool) menu: Options / Under The Hood / [Show cookies and website permissions] For related domains/sites type the suffix into the search box (like .foo.tv). Caveat: when you have a node (site or cookie) click-highlighted only use [Remove] to kill specific subtrees. Using [Remove All] will still delete cookies for all sites selected by search and waste your debugging session.
protected by Community♦ Jun 21 '12 at 6:47
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?