1

I want an event that work when I close browser only (not close tab or reload page).

I am using the following code :

$(window).on("unload",function(){
    localStorage.clear();
});

I have tried onunload & onbeforeunload, it works for close browser, reload page, close tabs, but for me I want an event that affects only close browser. Any recommended event (close browser only) for me.

stefanobaghino
  • 11,253
  • 4
  • 35
  • 63
Math 277
  • 21
  • 1
  • 1
  • 5
  • https://stackoverflow.com/questions/3888902/detect-browser-or-tab-closing – Brian Jan 30 '18 at 03:00
  • 1
    Possible duplicate of [Detect browser or tab closing](https://stackoverflow.com/questions/3888902/detect-browser-or-tab-closing) – LJHarb Jan 30 '18 at 04:05
  • 1
    both `onunload` & `onbeforeunload` is the same, but my case is i want event only **close browser** – Math 277 Jan 30 '18 at 06:45
  • `onunload` & `onbeforeunload` work for close browser, reload page, close tabs, but for me i want which event effect only close broswer – Math 277 Jan 30 '18 at 07:10

2 Answers2

0

You can only detect an unload of a page, not closing of the browser. It just cannot be done.

Jelmer Jellema
  • 1,072
  • 10
  • 16
0

There's no such event that exclusively works on browser close.

codejockie
  • 9,020
  • 4
  • 40
  • 46