Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 823
Speldosa Avatar asked Aug 03 '10 21:08

Speldosa


People also ask

How do I find cookies on a page?

For Google Chrome go to View > Developer > Developer Tools or CMD + ALT + I on Mac or F12 on Windows. ‍Now open the Application tab and check the cookies for each domain. Usually the cookies have names that resemble the name of the service they are being used by.

Can JavaScript access all cookies?

Client (browser) The JavaScript that downloads and executes on a browser whenever you visit a website is generally called the client-side JavaScript. It can access cookies via the Document property cookie . This means you can read all the cookies that are accessible at the current location with document.


2 Answers

You can list cookies for current domain:

function listCookies() {     var theCookies = document.cookie.split(';');     var aString = '';     for (var i = 1 ; i <= theCookies.length; i++) {         aString += i + ' ' + theCookies[i-1] + "\n";     }     return aString; } 

But you cannot list cookies for other domains for security reasons

like image 114
DixonD Avatar answered Oct 01 '22 01:10

DixonD


var x = document.cookie;  window.alert(x); 

This displays every cookie the current site has access to. If you for example have created two cookies "username=Frankenstein" and "username=Dracula", these two lines of code will display "username=Frankenstein; username=Dracula". However, information such as expiry date will not be shown.

like image 26
Speldosa Avatar answered Oct 01 '22 00:10

Speldosa