5

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?

Camilo Díaz Repka
  • 4,805
  • 5
  • 43
  • 68
Steve Claridge
  • 10,650
  • 8
  • 33
  • 35

3 Answers3

12

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

See an example here.

Community
  • 1
  • 1
Camilo Díaz Repka
  • 4,805
  • 5
  • 43
  • 68
  • 3
    Yeah, thanks! Just realised this too, must remember that there's a thing called Javascript lurking under jQuery. – Steve Claridge Dec 01 '10 at 14:50
  • 1
    [Here](https://www.inkling.com/read/javascript-definitive-guide-david-flanagan-6th/chapter-20/parsing-the-document-cookies) is another example of how to parse it. – Bentley4 Nov 01 '13 at 12:06
2

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.

Chris
  • 2,619
  • 6
  • 27
  • 34
0

Alternatively to the answers already provided, you could simply use the framework the folks at Mozilla created in their Document.cookie documentation.

Here is how it looks:

/*\
|*|
|*|  :: cookies.js ::
|*|
|*|  A complete cookies reader/writer framework with full unicode support.
|*|
|*|  Revision #1 - September 4, 2014
|*|
|*|  https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
|*|  https://developer.mozilla.org/User:fusionchess
|*|
|*|  This framework is released under the GNU Public License, version 3 or later.
|*|  http://www.gnu.org/licenses/gpl-3.0-standalone.html
|*|
|*|  Syntaxes:
|*|
|*|  * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
|*|  * docCookies.getItem(name)
|*|  * docCookies.removeItem(name[, path[, domain]])
|*|  * docCookies.hasItem(name)
|*|  * docCookies.keys()
|*|
\*/

var docCookies = {
  getItem: function (sKey) {
    if (!sKey) { return null; }
    return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
  },
  setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
    if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
    var sExpires = "";
    if (vEnd) {
      switch (vEnd.constructor) {
        case Number:
          sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
          break;
        case String:
          sExpires = "; expires=" + vEnd;
          break;
        case Date:
          sExpires = "; expires=" + vEnd.toUTCString();
          break;
      }
    }
    document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
    return true;
  },
  removeItem: function (sKey, sPath, sDomain) {
    if (!this.hasItem(sKey)) { return false; }
    document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
    return true;
  },
  hasItem: function (sKey) {
    if (!sKey) { return false; }
    return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
  },
  keys: function () {
    var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
    for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
    return aKeys;
  }
};

Source: developer.mozilla.org - Document.cookie - A little framework: a complete cookies reader/writer with full unicode support

benomatis
  • 5,536
  • 7
  • 36
  • 59