Skip to content Skip to sidebar Skip to footer

How To Update A Cookie Value (and Not Change The Expire Date)? Javascript

I have next code where I do: Check if my cookie exist if it not exist then I create the cookie called value inside else I read the cookie again and check if value is 1, here i wou

Solution 1:

While you can't directly check the expiry date, when you set the cookie up you can store the expiry date as part of the cookie. Then whenever you want to check the expiry date, you can look at that part of the cookie. And to change the cookie without affecting the expiry date, just recreate the cookie, replacing the expiry date.

document.cookie="username=John Doe Thu, 18 Dec 2013 12:00:00 UTC; expires=Thu, 18 Dec 2013 12:00:00 UTC";

You can always change the expiry date of the cookie when it is being created.


Solution 2:

There is no way to get expiration date of setted cookie in JavaScript, you can just get the value of cookie, so, I guess, without this knowledge you cant set cookie without replacing previous value of expiration.


Post a Comment for "How To Update A Cookie Value (and Not Change The Expire Date)? Javascript"