Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Read all domain cookies if you don't know their names

I'm using the jQuery cookie plugin to read/write/delete cookies. I'm using cookies to store points on a graph that the user plotted on top of a canvas. I'm allowing the user to store the plotted points along with a name in the cookie, I'm also listing the saved cookies so that they can redraw their saved points on the graph.

I was originally saving and reloading the points from cookies by naming each cookie with a sequential number $.cookie("_1"), $.cookie("_2"), etc and this worked. Problems start when user deletes a cookie and the sequential numbering breaks.

I would like to save the cookie using the name that the user gives to the plotted points, so basically saving cookies with arbitrary names. If I do this is it possible to read all domain cookies if I don't know their names?

like image 899
Steve Claridge Avatar asked Dec 01 '10 14:12

Steve Claridge


2 Answers

You don't need jQuery for this. You can read all your cookies by accessing document.cookie and parsing accordingly.

See an example here.

like image 136
Camilo Díaz Repka Avatar answered Sep 30 '22 17:09

Camilo Díaz Repka


I don't know about calling all domain cookies, but you could always store the cookie with a name you choose and then make the value of the cookie be "name,x,y" or something. Just an idea as an alternative to trying to pull all domain cookies.

EDIT:

Also, this cookies plugin wiki shows that you can easily get a filtered list of cookies. So you could throw on an identifier to the name of the cookie "mysite+name" and then use .slice to take it back off after you get your filtered list.

like image 45
Chris Avatar answered Sep 30 '22 18:09

Chris