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
2010-08-03 21:09:50
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
2010-08-03 21:14:30
Good resource: http://www.w3schools.com/JS/js_cookies.asp
tur1ng
2010-08-03 21:16:07
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
2010-08-03 21:22:02
+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
2010-08-03 21:12:53
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
2010-08-03 22:17:22
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
2010-08-03 21:13:25
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
2010-08-03 22:57:52