can you all suggest some approaches by which i can clear local storage on last tab close and on browser close..
can you all suggest some other approaches by which i can clear local storage on last tab close and on browser close.
I used local storage and session storage to maintain a array in localstorage of sessions open and close and when the array is empty i clear Local Storage. but it is not working on the condition when user shutdown its pc without closing its browser or tab.
my code…
@HostListener('window:load', ['$event'])
loadHandler(event: Event) {
let hash = 'tab_' + formatDate(new Date(), 'yyyy/MM/dd-hh:mm:ss:ms', 'en');
sessionStorage.setItem('TabHash', hash);
let tabs = JSON.parse(localStorage.getItem('TabsOpen')) || [];
console.log(tabs);
tabs.push(hash);
localStorage.setItem('TabsOpen', JSON.stringify(tabs));
}
@HostListener('window:unload', ['$event'])
beforeunloadHandler(event: Event) {
let hash = sessionStorage.getItem('TabHash');
let tabs = JSON.parse(localStorage.getItem('TabsOpen')) || [];
tabs.pop();
localStorage.setItem('TabsOpen', JSON.stringify(tabs));
setTimeout(() => {
let left_tabs_Open = localStorage.getItem('TabsOpen');
if (left_tabs_Open == '[]') {
window.localStorage.clear();
}
});
}