0

Hi I have used localStorage to store the value of a variable.The problem is that localStorage never resets from what I managed to find on the internet.I am interested to reset localStorage when the browser is closed.

Is there a way to do that?

I have tryed using $(window).unload from jQuery but that detects changes related to the window and it resets the localStorage variable to soon.

Nistor Alexandru
  • 5,309
  • 9
  • 46
  • 71

2 Answers2

2

Read my answer in which I explained how to detect browser close, $(window).unload doesn't work always.

<script>
 window.onbeforeunload = function (e) {

 if (localStorage) {
    localStorage.clear();
 }
 };
</script>

But, yes I would recommend to use Cookies or session variable which would be destroyed on browser close instead of localStorage.

Community
  • 1
  • 1
gopi1410
  • 6,567
  • 9
  • 41
  • 75
  • why not `if (localStorage) localStorage.clear()` in all cases? – mplungjan Jun 02 '12 at 17:11
  • 1
    @mplungjan the question says **I am interested to reset localStorage when the browser is closed.** – gopi1410 Jun 02 '12 at 17:13
  • @mplungjan why not just `localStorage.clear()` directly? :P both will cause error if `localStorage` is not supported. The troll here is that `if( anything )` will cause `ReferenceError` if `anything` is not defined – Esailija Jun 02 '12 at 17:13
  • 1
    I meant: `window.onbeforeunload = function() { if (window.localStorage) localStorage.clear()}` to be completely clear. Gopi's "for safari" part will also fail if there is no localStorage defined – mplungjan Jun 02 '12 at 17:15
  • 1
    There's indeed no need for the `if (e)` block. That is only necessary if you need to set a return value, as with the confirm to close question. Instead, you should check whether `localStorage` is supported by the browser, as explained by @mplungjan. – Mattias Buelens Jun 02 '12 at 17:16
  • @mplungjan yup maybe, but I was in doubt that if alert does not work for window.onbeforeunload, so this **may** not work, thats why I added this piece of code. – gopi1410 Jun 02 '12 at 17:17
  • 1
    @mplungjan ok, got it. my bad. The `if(e)` block is only necessary if you need to set a return value, got that now. & I edited the answer to make it appropriate. – gopi1410 Jun 02 '12 at 17:19
  • This will clear `localStorage` any time the page reloads or relocates, which is what happens with javascript memory already, I don't see the point. – Esailija Jun 02 '12 at 17:25
  • @Esailija didn't get you. localStorage doesn't clear on page reload. – gopi1410 Jun 02 '12 at 17:27
  • wait what the ?? @gopi1410 Esailija is suggest sessionStorage.. not localStorage – ShrekOverflow Jun 02 '12 at 17:28
  • @Esailija yeah I get you, the asker wanted to accomplish it with localStorage, so I answered this. It is the same as sessionStorage.. :P – gopi1410 Jun 02 '12 at 17:29
  • @gopi1410 no, he can accomplish this with `{}`. and `sessionStorage` is not the same thing, it only clears out when you actually close the browser or tab itself. this clears out `localStorage` anytime the tab refreshes or changes location. I.E. no different to using a javascript object with `{}` – Esailija Jun 02 '12 at 17:30
  • Well actually, not different to javascript objects is false, it will write to disk (and synchronously, too) instead into memory. – Esailija Jun 02 '12 at 17:38
2

You probably want to use window.sessionStorage instead.

sessionStorage

This is a global object (sessionStorage) that maintains a storage area that's available for the duration of the page session. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated.

Esailija
  • 138,174
  • 23
  • 272
  • 326