Javascript jQuery:如果您不知道名称,请阅读所有域 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4325175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: Read all domain cookies if you don't know their names
提问by Steve Claridge
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.
我正在使用 jQuery cookie 插件来读取/写入/删除 cookie。我正在使用 cookie 将点存储在用户绘制在画布顶部的图形上。我允许用户将绘制的点与名称一起存储在 cookie 中,我还列出了保存的 cookie,以便他们可以在图表上重绘保存的点。
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.
我本来节约和命名每个cookie一个序列号重装从饼干点$.cookie("_1")
,$.cookie("_2")
等这个工作。当用户删除 cookie 并且顺序编号中断时,问题就开始了。
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?
我想使用用户为绘制点提供的名称保存 cookie,因此基本上使用任意名称保存 cookie。如果我这样做,如果我不知道它们的名字,是否可以读取所有域 cookie?
回答by Camilo Díaz Repka
回答by Chris
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.
我不知道如何调用所有域 cookie,但您始终可以使用您选择的名称存储 cookie,然后将 cookie 的值设为“name,x,y”或其他内容。只是一个想法,作为尝试提取所有域 cookie 的替代方法。
EDIT:
编辑:
Also, this cookies plugin wikishows 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.
此外,此 cookie 插件 wiki显示您可以轻松获取过滤的 cookie 列表。因此,您可以将标识符添加到 cookie 的名称中"mysite+name"
,然后.slice
在获得过滤列表后使用它来取消它。
回答by webeno
Alternatively to the answers already provided, you could simply use the framework the folks at Mozilla created in their Document.cookie
documentation.
除了已经提供的答案,您可以简单地使用 Mozilla 人员在其Document.cookie
文档中创建的框架。
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*([^;]*).*$)|^.*$"), "")) || 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*(?:\=[^;]*)?(?:|$)/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 - 一个小框架:一个完整的 cookie 读写器,具有完整的 unicode 支持