无法从jquery cookie加载超过23项
我正在寻找一种将数组保存/加载到jquery cookie的方法,并使用了这个问题的最上面的答案 我可以在cookie列表中添加任意数量的项目,并在检查数组长度时获得正确的数字。但当我刷新页面并加载cookie列表时,数组中最多只能得到23项,并且找不到原因 这是我的密码无法从jquery cookie加载超过23项,jquery,arrays,cookies,Jquery,Arrays,Cookies,我正在寻找一种将数组保存/加载到jquery cookie的方法,并使用了这个问题的最上面的答案 我可以在cookie列表中添加任意数量的项目,并在检查数组长度时获得正确的数字。但当我刷新页面并加载cookie列表时,数组中最多只能得到23项,并且找不到原因 这是我的密码 //This is not production quality, its just demo code. var cookieList = function(cookieName) { //When
//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);
我添加的项目(即链接)如下
浏览器的标准cookie大小是4096。您可能在以下一种或两种情况下触发此限制: 1个cookie中数据的大小限制
多个尚未过期的Cookie,总计4096个 HTML5和Jquery都支持本地存储以绕过此问题
这些物品有多长?您能够存储的最长cookie字符串有多长,与第一次失败的长度相比?你可能只是遇到了。我不知道确切的情况,但我只是用较短的字符串测试了它,并且能够在cookie中存储更多的项目,所以这看起来确实是一个大小限制问题。没想到会这么快达到极限。谢谢谢谢!使用本地存储修复了我的问题!那么请将此标记为已接受的答案。谢谢
packCollectorCookieList.add($(this).parent('li').data('link'));