1

My requirements are:

  1. Have to detect browser close event and it should not execute onpage refresh.

  2. If code works only on browser close then have to clear localstorage.

  3. Already i tried with onbeforeunload but it is executing on browser close event and page refresh event.

  4. So my code only works on browser close event not on page refresh (F5 or clicking on refresh symbol).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
user3523448
  • 35
  • 1
  • 2
  • 8

2 Answers2

8

You cannot tell the difference between the browser being closed and the window/tab containing your page being refreshed. The information simply isn't available.

For your specific use case, it sounds like you might be able to use session storage rather than local storage. The point of session storage is that it expires when the user is done with the page. You can try that out with this example:

display("Existing <code>sessionStorage.foo</code>: " + sessionStorage.foo);
sessionStorage.foo = new Date().toISOString();
display("New value set: " + sessionStorage.foo);

For me on Firefox, Chrome, and IE9 (so presumably IE9+), refreshing doesn't clear session storage, but closing the tab does.

Alternately, you could set a timestamp in local storage when the page is being unloaded (from onbeforeunload), and when you come back to the page in the future, if that timestamp is sufficiently out of date, clear the storage and start fresh. That doesn't clear the store when you leave the page, but does act as though it did when you come back after an interval.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • but scope of session storage is only to a tab, so you cannot use multiple tab functionality. – Prashant Pokhriyal Jul 14 '17 at 10:48
  • @PrashantPokhriyal: There's nothing about multiple tab functionality in the question. But it's more complicated than that in any case: If a new tab (in the same origin) is opened by an existing tab, they share session storage, because the new tab's browsing context isn't the top level browsing context (because its `opener` is the old tab). So clicking a `target="_blank"` link or using `window.open` in the source tab means the target tab shares its session storage. – T.J. Crowder Jul 14 '17 at 11:51
  • But If a new tab (in the same origin) is **NOT** opened by an existing tab, then? – Prashant Pokhriyal Jul 14 '17 at 13:05
  • @PrashantPokhriyal: Again, completely irrelevant to the question. But yes, if you open a new tab in the same origin via another mechanism, they do not share a top-level browsing context, and thus although they share local storage, they do not share session storage. But the question is about refreshing the current tab, not opening a new one (much less doing so via something outside the current one), and session storage is maintained in that case. – T.J. Crowder Jul 14 '17 at 13:20
0

There is no event that gets fired when only the browser is closed. The closest is the onbeforeunload event which gets emitted when the window is about to unload its resources.

Miguel Mota
  • 20,135
  • 5
  • 45
  • 64
  • Yes,but i have to clear localstorage only on close event , when i used with onbeforeunload ,it is executing for refresh event also, so must i want to exclude refresh event..Required and needed,,plz find solution – user3523448 Aug 31 '14 at 07:37
  • Take a look at session storage https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage – Miguel Mota Aug 31 '14 at 07:45