开发者

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

开发者 https://www.devze.com 2023-01-28 13:34 出处:网络
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

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?


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

See an example here.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消