2

I googled it before, but I could not find any solution about detecting only window closing event instead of tab closing and refresh events in JavaScript.

I used this code below which works for all cases: closing window, closing tab and also refresh page:

window.addEventListener("beforeunload", function (e) { 
.. 
}

I could not find a solution to differentiate between the different cases, because my need is that I want only to detect window close not tab neither refresh.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
sohaieb azaiez
  • 768
  • 9
  • 20
  • I am not sure, but I assume that this is not possible. AFAIK a browser window is just a container for browser tabs. Closing a browser window will trigger closing all tabs but there is no message for this event. – Thomas Sablik Oct 22 '20 at 12:08
  • 1
    There is no way to distinguish between a tab close and browser / window close actions. – Vladislav Ladicky Oct 22 '20 at 12:12
  • 1
    but what about refresh action ? (thank you for your answers). – sohaieb azaiez Oct 22 '20 at 12:13
  • many discussions on this topic in stackoverflow. Some people have used workaround like below : https://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event/26275621#26275621 – pavanjoshi Oct 22 '20 at 12:19
  • @pavanjoshi thank you but i saw these answers before but this did not help too, that's why i asked – sohaieb azaiez Oct 22 '20 at 13:29
  • 1
    ok @sohaieb, i just meant that it is not possible directly to listen on it, so you need to find a workaround just suiting your use case. – pavanjoshi Oct 23 '20 at 04:12

0 Answers0