Hi I have cookie which has value of serialized params.
i.e)criteria = "name=praveen&age=25&studying=false&working=true&something=value"
Now I have to update name = praveenkumar,age = 24,something = null in this cookie string. If value is null(something = null) then it should be removed from cookie. The following is the code I am using.
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("&"));
};
It is working. But I am concerned about performance if the size of cookie become more.