I'm using this code to delete data when the user close the window
window.onbeforeunload = function() {
localStorage.clear();
};
And it's working fine, but I've just realize that this method is deleting data after refreshing the page with 'F5' also. Is there another way to delete data only when the user close the window or tab?
What am I trying to do?
I should not use cookies. I need to persist my session over the tabs in any navigator, but:
- When the user close window the data has to be deleted
- When the user press 'F5' key the data shouldn't be deleted
Why am I not using sessionStorage?
Because when you are already signed on my website and suddenly you open another tab with the same direction my website should alert the user that he has an active session.