0
嗨我有cookie的序列化params的价值。如何使用jquery更新序列化的Cookie值?
i.e)criteria = "name=praveen&age=25&studying=false&working=true&something=value"
现在我必须更新名称= praveenkumar,年龄= 24,东西= NULL这个cookie字符串。如果value为null(something = null),那么它应该从cookie中移除。以下是我正在使用的代码。
updateCookie({'name':'praveenkumar','age':24,'something':null})
var updateCookie = function(params) {
// split the cookie into array
var arr = $.cookie('criteria').split("&");
//loop through each element
for(var i = arr.length - 1; i >= 0; i--) {
// getting the key i.e) name,age
var key = arr[i].split("=")[0];
// if the key is in given param updating the value
if(key in params) {
// if vale null remove the element from array or update the value
if(params[key] !== null) {
arr[i] = key+"="+params[key];
}
else {
arr.splice(i, 1);
}
}
}
// join array by & to frame cookie string again
$.cookie('criteria' , arr.join("&"));
};
它正在工作。但是如果cookie的大小变得更大,我会关心性能。
@davidkonard这真的很有帮助。性能事件是存储在cookie中的参数数量可能增加,通过每个参数枚举可能会花费更多。 – Praveenkumar