user1010701
user1010701

Reputation: 59

setting expiry on one value within a jquery cookie

I'm using a jQuery cookie to set 3 value say x,y and z and I want z to expire after 365 days but not expire x and y.

$.cookie("MyTestCookie", xyz, { expires: 999999 });  

I have split the values using:

var xVal =  my_cookie_value.substring(0);
var yVal =  my_cookie_value.substring(1);
var zVal =  my_cookie_value.substring(2);

$.cookie("MyTestCookie", zVal, { expires: 365 }); 

Thakyou in advance

Upvotes: 0

Views: 169

Answers (1)

Manse
Manse

Reputation: 38147

What you are doing is

$.cookie("MyTestCookie", xyz, { expires: 999999 });  
$.cookie("MyTestCookie", zVal, { expires: 365 }); 

this is just replacing the first cookie with the second and updates the value and expiration as the names (MyTestCookie) are the same

What you can do is this :

$.cookie("X-MyTestCookie", xVal, { expires: 99999 }); 
$.cookie("Y-MyTestCookie", yVal, { expires: 99999 }); 
$.cookie("Z-MyTestCookie", zVal, { expires: 365 }); 

notice the cookie names are different (X-MyTestCookie, Y-MyTestCookie and Z-MyTestCookie) - this places the values in different cookies ...

or

$.cookie("MyTestCookie", xVal + "%" + yVal, { expires: 99999 }); 
$.cookie("Z-MyTestCookie", zVal, { expires: 365 }); 

In the first line the % character is used as a delimiter to separate the values - you then need to split the values when you get the cookie.

Upvotes: 1

Related Questions