How to update cookie expiration time with JavaScript?

javascript

Solution

By "sliding time" I'm assuming you want the cookie to renew itself whenever the visitor reloads the page. Just reset the cookie every time the page loads (i.e. every time the JavaScript code is executed).

https://developer.mozilla.org/en-US/docs/DOM/document.cookie

However if you're looking for something more long term with more capacity then I would recommend using `localStorage`:

https://developer.mozilla.org/en-US/docs/DOM/Storage#localStorage

Problem

I want sliding time for expiration time of cookie. This can be done by getting cookie and again setting with new expiration time and deleting previous cookie. But , I want to know better way than this . I need some good solution.

Original source