【问题标题】:jQuery: Read all domain cookies if you don't know their namesjQuery:如果您不知道它们的名称,请阅读所有域 cookie
【发布时间】:2011-05-18 12:46:40
【问题描述】:

我正在使用 jQuery cookie 插件来读取/写入/删除 cookie。我正在使用 cookie 将点存储在用户在画布上绘制的图形上。我允许用户将绘制的点连同名称一起存储在 cookie 中,我还列出了保存的 cookie,以便他们可以在图表上重新绘制他们保存的点。

我最初是通过用序列号$.cookie("_1")$.cookie("_2") 等命名每个 cookie 来保存和重新加载 cookie 中的点,这很有效。当用户删除 cookie 并且顺序编号中断时,问题就开始了。

我想使用用户为绘制点指定的名称保存 cookie,因此基本上保存具有任意名称的 cookie。如果我这样做如果我不知道它们的名字,是否可以读取所有域 cookie?

【问题讨论】:

    标签: javascript jquery cookies jquery-plugins


    【解决方案1】:

    我不知道调用所有域 cookie,但您始终可以使用您选择的名称存储 cookie,然后将 cookie 的值设置为“名称、x、y”或其他内容。只是一个想法,作为尝试提取所有域 cookie 的替代方案。

    编辑:

    此外,this cookies plugin wiki 表明您可以轻松获得经过过滤的 cookie 列表。因此,您可以在 cookie 名称上添加标识符 "mysite+name",然后在获得过滤列表后使用 .slice 将其取消。

    【讨论】:

    • 是的,可以这样做,如果无法读取所有cookie,则将重写cookie的顺序编号。
    • 我发现了一些可能有帮助的东西,所以我编辑了我的答案来展示它。
    【解决方案2】:

    你不需要 jQuery。您可以通过访问 document.cookie 并进行相应的解析来读取所有 cookie。

    查看示例here

    【讨论】:

    • 好的,谢谢!刚刚也意识到了这一点,一定要记住在jQuery下面潜伏着一个叫Javascript的东西。
    • Here 是另一个如何解析它的例子。
    【解决方案3】:

    除了已经提供的答案之外,您还可以简单地使用 Mozilla 的人们在他们的 Document.cookie 文档中创建的框架。

    这是它的外观:

    /*\
    |*|
    |*|  :: 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;
      }
    };
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-01
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多