无法从 jquery cookie 加载超过 23 个项目



我正在寻找一种将数组保存/加载到 jquery cookie 的方法,并使用了这个问题的最高答案如何在 jquery cookie 中存储数组? 我可以根据需要将任意数量的项目添加到 cookie 列表中,并在检查数组长度时获得正确的数字。但是一旦我刷新页面并加载 cookie 列表,我最多只能得到数组中的 23 个项目,并且找不到原因。

这是我的代码

//This is not production quality, its just demo code.
var cookieList = function(cookieName) {
//When the cookie is saved the items will be a comma seperated string
//So we will split the cookie by comma to get the original array
var cookie = $.cookie(cookieName);
//Load the items or a new array if null.
var items = cookie ? cookie.split(/,/) : new Array();
//Return a object that we can use to access the array.
//while hiding direct access to the declared items array
//this is called closures see http://www.jibbering.com/faq/faq_notes/closures.html
return {
"add": function(val) {
//Add to the items.
items.push(val);
//Save the items to a cookie.
//EDIT: Modified from linked answer by Nick see 
//      http://stackoverflow.com/questions/3387251/how-to-store-array-in-jquery-cookie
$.cookie(cookieName, items.join(','));
},
"remove": function (val) { 
//EDIT: Thx to Assef and luke for remove.
indx = items.indexOf(val); 
if(indx!=-1) items.splice(indx, 1); 
$.cookie(cookieName, items.join(','));        },
"clear": function() {
items = [];
//clear the cookie.
//$.cookie(cookieName, '');
$.removeCookie(cookieName);
},
"items": function() {
//Get all the items.
return items;
}
}
}
var packCollectorCookieList = new cookieList("xWeaselPackCollector"); // all items in the array.
var compareListCollector = packCollectorCookieList.items();
alert(compareListCollector.length);

我添加项目(这是链接(,例如

packCollectorCookieList.add($(this).parent('li').data('link'));

浏览器的标准 cookie 大小为 4096。您可能会在以下一项或两项中触发此限制:

1 个 cookie 中数据的大小限制 多个未过期的 cookie
加起来为 4096。

HTML5和Jquery都支持本地存储来绕过这个问题。

克服饼干大小限制

最新更新