views:

51

answers:

5

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.

A: 

No there isn't. You can only read information associated with the current domain.

tur1ng
Damn :) Ok...Follow up questions: 1. Is there any way to check if a specific cookie exists (with a certain name that you know)?2. Is there any kind of manual for JavaScript where for example the document.cookie class is desribed in detail?
Speldosa
Good resource: http://www.w3schools.com/JS/js_cookies.asp
tur1ng
I think so too.However, I would like to get hold of something equivalent of this: http://download.oracle.com/javase/1.4.2/docs/api/overview-summary.html
Speldosa
A: 

(For the domain, not the page as tur1ng points out)

http://snipplr.com/view/15641/list-cookies-in-javascript/

monojohnny
+4  A: 

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

DixonD
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 :)
Speldosa
You can mark my answer as accepted if it suits you :)
DixonD
Sure thing! Thanks!
Speldosa
A: 

No.

The only API browsers give you for handling cookies is getting and setting them via key-value pairs. All browsers handle cookies by domain name only.

Accessing all cookies for current domain is done via document.cookie.

Yuval A
A: 
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.

Speldosa