16

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.

Community
  • 1
  • 1
AlexStack
  • 16,766
  • 21
  • 72
  • 104
  • 3
    Store an _expiration date_ into the storage then take decisions such as cleaning data! – Florent Oct 22 '12 at 12:58
  • 3
    Some bad news for you, friend. Looks like it is up to the user. http://stackoverflow.com/questions/2326943/when-do-items-in-html5-local-storage-expire I am sure you can store timestamps, compare and reject if it is too old though. – kush Oct 22 '12 at 13:00
  • I agree with @kush. store the expiry timestamp and decide when reading. – San Oct 22 '12 at 13:02
  • Great news! It's up to the user. See my answer here: http://stackoverflow.com/a/30730835/873650 – Fernando Fabreti Jun 09 '15 at 11:36

2 Answers2

23

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.

Some Guy
  • 15,854
  • 10
  • 58
  • 67
1

The Storage model was meant to be a cache for scripts; not of a spur-of-the-moment data holder. Infact sessionStorage should clear as soon as the protocol+domain is navigated away from. With that said, the simplest way would be to store and check a 'hold-until' value at each page view, and update/clear-storage it as needed

SReject
  • 3,774
  • 1
  • 25
  • 41