Javascript get cookie names

I need to get all the cookies from the browser

nayagi, unfortunately if we do answer your question you would have software that is nothing less than MALWARE.

@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.

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.

If you truly need all cookies, you have to create and install a browser extension to do that. Cookies used for logins are considered sensitive and browsers do their best to keep those secret.

9 Answers 9

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 

this doesnt catch the space after the semicolon ("; ") so keys have a space at the front. use /; ?/ when splitting to remove them?

  1. You can't see cookies for other sites.
  2. You can't see HttpOnly cookies.
  3. All the cookies you can see are in the document.cookie property, which contains a semicolon separated list of name=value pairs.

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.

Actually, 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.

thank you. it's opening my eyes. I think any kind of app related to them will do, including login using social media account app.

@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.

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

let c = document.cookie.split(";").reduce( (ac, cv, i) => Object.assign(ac, ), <>); console.log(c); 

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.

Источник

How can I list all cookies for the current page with Javascript?

Is there any way to, with help of Javascript, list all cookies associated with the current page? That is, if I don't know the names of the cookies but want to retrieve all the information they contain.

Please clarify if by "certain page", you mean "the page the user is currently on". DixonD's answer is spot on for current-page cookies. All other domains are offlimits for security reasons.

Note that this is something that you actually probably want to disable with HttpOnly . This is because it makes XSS attacks a lot less powerful.

11 Answers 11

You can list cookies for current domain:

function listCookies() < var theCookies = document.cookie.split(';'); var aString = ''; for (var i = 1 ; i return aString; > 

But you cannot list cookies for other domains for security reasons

I must have been clumsy in my description of the problem. What I wanted to do was to get a list of all the cookies created by any html-documents in the same catalogue. In the html-document I simply added the following code: var x = document.cookie; window.alert(x); . and I could see all the cookies I had created. Sorry if I expressed myself in an unclear way. Thanks for all the quick answers though. I already like this site 🙂

Experimental Spec (poor browser support as of Apr 2023, use 2022 answer):

const cookies = await cookieStore.getAll() // Then. Object.fromEntries(cookies.map(() => [name, value])) // OR cookies.reduce((cs, c) => (), <>) 

Updated for 2022

tl;dr - Here's a one liner:

Object.fromEntries(document.cookie.split('; ').map(c => c.split('='))) 

Note: You cannot get http-only cookies in browser code.

Here's the same thing, explained:

const getCookieMap = () => < // Cookies are generally separated by a "; " // https://stackoverflow.com/a/4843598/2968465 const cookieList = document.cookie.split('; '); // A key-value pair in the cookie list is separated by a "=" // We pass a function to cookieList.map that will return // an array of tuples, like Javascript get cookie names const cookieToObjEntry = cookie =>cookie.split('=') const cookieEntries = cookieList.map(cookieToObjEntry) // Such an array can be passed to Object.fromEntries to // obtain an object with all cookie key-value pairs as // the keys and values of an object // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries return Object.fromEntries(cookieEntries) // So, for a cookies stored as "c1=v1; c2=v2", you'll get // an object like `` > 

Older answers

Many people have already mentioned that document.cookie gets you all the cookies (except http-only ones).

I'll just add a snippet to keep up with the times.

document.cookie.split(';').reduce((cookies, cookie) => < const [ name, value ] = cookie.split('=').map(c =>c.trim()); cookies[name] = value; return cookies; >, <>); 

The snippet will return an object with cookie names as the keys with cookie values as the values.

Slightly different syntax:

document.cookie.split(';').reduce((cookies, cookie) => < const [ name, value ] = cookie.split('=').map(c =>c.trim()); return < . cookies, [name]: value >; >, <>); 

Edit: Someone correctly pointed out that you'll face issues if your cookie key or value has an = in it. Maybe consider using escape sequences to mitigate this?

Источник

Читайте также:  Java net connection exception connection timed out no further information
Оцените статью