1

I have the following variable localStorage.timesDisplayed in the browser's local storage.

I am wondering if there is a way to clear that variable with Javascript when browser is restarted.

I can use : delete localStorage.timesDisplayed to delete the variable, but I am not sure how to detect that the browser has been restarted on the page load.

callback
  • 3,981
  • 1
  • 31
  • 55
  • can you just clear it out on page load? also why use local storage for a temporary value? don't you want a plain variable for that? – Daniel A. White Sep 22 '15 at 14:14
  • You're probably changing the `localStorage` to `sessionStorage` as it automatically does exactly what you want to do. – Buzinas Sep 22 '15 at 14:14
  • @Buzinas on F5, session storage is maintained. – Daniel A. White Sep 22 '15 at 14:14
  • 1
    @DanielA.White but that's exactly what he wants. He want to delete the variable when the browser is *restarted*, and not when the page is *reloaded*. – Buzinas Sep 22 '15 at 14:15
  • yes, that's what I want.. I want to keep the variable alive on page load. But delete it only on browser restart (not page reload) – callback Sep 22 '15 at 15:48
  • Have a look at https://stackoverflow.com/a/53307588/2828611, this might answer your question – inetphantom Jul 09 '20 at 08:24

1 Answers1

-3

You could use sessionStorage instead. It will clear when the user exits your site.

https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

Brett
  • 1
  • This is not true. Despite its name, sessionStorage does NOT clear automatically when you close your browser. – Joe Leonard Apr 04 '18 at 17:41
  • 1
    https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage tells: "Closing a tab/window ends the session and clears objects in sessionStorage". But sessionStorage may not fit if you want to share data between tabs. – Dmitry Dmitriy Sep 25 '19 at 09:44