Rectangle 27 0

reactjs How to detect whether browser tab closed or browser window in javascript?


if (sessionStorage.clickcount) {
    sessionStorage.clickcount = Number(sessionStorage.clickcount) + 1;
} else {
    sessionStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
sessionStorage.clickcount + " time(s) in this session.";
localStorage.removeItem(key);

I grab those example code from Here

I want to clear data on browser window close not on tab close. So, If i use sessionStorage then it'll clear on tab close also which i don't want to do.

Use Session Storage in the browser. Here you don't want to capture the browser tab closing event. The Session Storage data persist for a session of opened tab

but in your question you mention "whether browser tab is closed or browser window" therefore I post that answer. I will edit my answer. please wait

sorry for misrepresentation. I just wanted to detect if it is a browser tab or browser window closed by user. if it is browser window then clear localstorage else retain it. So i am not founding the way for that decision point tab or window. Thanks

Note