2010-12-01 71 views
5

我正在使用jQuery cookie插件来读取/写入/删除cookie。我使用cookie将点存储在用户绘制在画布上的图形上。我允许用户在cookie中存储绘制的点以及名称,我还列出了保存的cookie,以便他们可以在图形上重新绘制其保存的点。jQuery:如果您不知道名称,请阅读所有域Cookie

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

我想使用用户给出的点名保存cookie,所以基本上用任意名称保存cookie。如果我这样做如果我不知道他们的名字,是否可以读取所有的域名cookie?

回答

12

你不需要这个jQuery。您可以通过访问document.cookie并相应地解析来阅读所有Cookie。

查看示例here

2

我不知道如何调用所有域cookie,但是您可以随时使用您选择的名称来存储cookie,然后使cookie的值为“name,x,y”或其他。只是一个想法,作为尝试提取所有域cookie的替代方案。

编辑:

此外,this cookies plugin wiki表明,你可以很容易地得到饼干的过滤列表。因此,您可以在cookie的名称上输入标识符"mysite+name",然后在获得过滤列表后使用.slice将其取消。

+0

呀,能做到这一点,是要改写饼干的顺序编号,如果所有的人的阅读是不可能的。 – 2010-12-01 14:35:57

+0

我发现了一些可能有用的东西,所以我编辑了我的答案以显示它。 – Chris 2010-12-01 14:44:02

0

除了已经提供的答案外,您可以简单地使用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

相关问题