Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make localStorage or sessionStorage expire like cookies [duplicate]

Possible Duplicate:
When do items in HTML5 local storage expire?

My web application uses client side storage (sessionStorage and localStorage). However, I want this data to be expired after some time (eg. 2 days). It was a super easy task with expiration date of cookies but I don't know how to set an expiration date for HTML5 storage.

Any ideas how to solve this problem? Efficiency and simplicity is important.

like image 522
AlexStack Avatar asked Oct 22 '12 12:10

AlexStack


People also ask

Can I set expiry for local storage?

LocalStorage has no expiration time, Data in the LocalStorage persist till the user manually delete it.

Does sessionStorage expire?

sessionStorage is similar to localStorage ; the difference is that while data in localStorage doesn't expire, data in sessionStorage is cleared when the page session ends. Whenever a document is loaded in a particular tab in the browser, a unique page session gets created and assigned to that particular tab.

Which is better localStorage or cookie?

Cookies are intended to be read by the server, whereas localStorage can only be read by the browser. Thus, cookies are restricted to small data volumes, while localStorage can store more data.

What is the difference between localStorage sessionStorage session and cookies?

For most cases, we use the localStorage object if we want some data to be on the browser. If we want it on the server, then we use cookies, and the sessionStorage is used when we want to destroy the data whenever that specific tab gets closed or the season is closed by the user.


1 Answers

The best you can do is to set a timestamp in storage, and if the user visits your site after a specified amount of time, then you can delete the stored data.

sessionStorage should work the same way, except when it doesn't even last long enough for your timed expiration. In that case, it'll expire sooner.

like image 81
Some Guy Avatar answered Sep 19 '22 15:09

Some Guy