Reputation: 21212
If I go to a website, open the console and type:
document.cookie.split(";")
["__atuvc=1%7C36", " openid_provider=facebook", " splitVar=Get Alerts!", " _ga=GA1.2.517247621.1408458879"]
I get all of the cookies back. I saw once a while ago someone showing me something in a different program (I think it was mongoDB) where they applied a .pretty method that would do this to the above:
__atuvc=1%7C36",
"openid_provider=facebook",
"splitVar=Get Alerts!",
"ga=GA1.2.517247621.1408451239"
Does such a method exist? It would just let me see with ease and convenience, the cookies on a website.
Put another way is there a way to neatly view name value pairs (Is a cookie an object?) on separate lines?
Upvotes: 0
Views: 43
Reputation: 25634
document.cookie.replace(/;/g,',\n')
If you want to please your eyes with some space, you could add that:
document.cookie.replace(/;/g,',\n').replace(/=/g,'\t=\t');
Here, the first parameter of the .replace()
method is a regular expression (Regex).
The 'g' is an option that says to replace multiple occurences of the expression instead of just the first one.
In the first example, ,\n
is a colon followed by a line break, replacing every semi colon. In the second one, we insert tabs around the =
sign.
Upvotes: 2
Reputation: 608
You can easily turn the string document.cookie
returns into an object, which will be readable enough in any console.
var cookies = decodeURIComponent(document.cookie).split(";"), res = {};
cookies.forEach(function(cookie){
var parts = cookie.split("=");
res[parts[0]] = parts[1];
});
console.log(res);
Upvotes: 1