kumara
kumara

Reputation: 937

how to find tab close and clear local storage

I am using onbeforeunload function in javascript but problem is that if browser is refresh then also clear local storage . I want to only clear local storage when browser close not refresh browser

window.onbeforeunload = function(e) {
        localStorage.setItem('isLoggedIn', 'false');
      };

Upvotes: 0

Views: 375

Answers (2)

Md. Abu Sayed
Md. Abu Sayed

Reputation: 2486

You can be needed to learn about local storage, session storage, and Cookies. there are 3 options use for store data but it's one of the different another concept.

  1. local storage store locally and it's permanent if you not remove this or clear browser.
  2. session storage use for containing a status or a token or a user some information.
  3. when you close the browser then session storage remove automatically. (note: if you went any running time you can change it like as logout).
  4. Cookies is another powerful way to store data in the browser or client site. it also allows the time schedule or date. when expiring time or date then it's destroyed automatically.

there are 3 storage are the different their concept and work it's also different storage capacity each other.

enter image description here

Session storage operation:

sessionStorage.SessionName = "SessionData" ,

sessionStorage.getItem("SessionName") and

sessionStorage.setItem("SessionName","SessionData");

enter image description here

Cookie example simple operation:

enter image description here

enter image description here

you can also learn about more:

https://www.quora.com/What-is-the-difference-between-sessionstorage-localstorage-and-Cookies

https://developer.mozilla.org/en-US/docs/Mozilla/Gecko/DOM_Storage_implementation_notes

https://www.w3schools.com/js/js_cookies.asp

=== Thanks ===

Upvotes: 0

jro
jro

Reputation: 940

You can use sessionStorage instead. It is automatically cleared when the browser is closed.

Example:

sessionStorage.setItem("isLoggedIn","true"); // Value will be set to null when browser is closed

Upvotes: 2

Related Questions