0

Is there a way where we can clear the localStorage item only on browser closed and not on tab closed.I have been reading many articles and I couldn't find any relevant solution, so thought of asking here.

My requirement is I need to retain the data on localStorage item on tab closed, but need to clear the data on browser closed.

Below code works on both tab and browser closed.

window.onunload = () => {}
Joshua
  • 3,055
  • 3
  • 22
  • 37
Abhishek
  • 517
  • 3
  • 18

1 Answers1

0
window.localStorage.removeItem('keyName');

Reference

How to delete a localStorage item when the browser window/tab is closed?

Wayne
  • 11
  • 2
  • I am not sure if you actually need to use SessionStorage or not – Wayne Jun 11 '20 at 10:18
  • this wont help, as it is simply removing the key.Please double check my question. My ask is simple. Clear the localStorage only on browser closed and not on tab closed.@wayne2 – Abhishek Jun 11 '20 at 13:08