I am working on mobile browser about 3 months and now i have a boring problem with beforeunload
and unload
. In two situation they do not work clearly. When I close the tab, the browser often calls them. But while user closes the browser with some routine why doesn't it trigger these events at all? What is the problem?
Asked
Active
Viewed 4,580 times
9

Victor
- 3,669
- 3
- 37
- 42

sina zarei
- 87
- 1
- 5
-
according to the MDN docs, `beforeunload` and `load` events are supported since Chrome for Androied version 18 and Firefox for Android version 4. please provide the minimal portion of the related code so we can help you better. – Maher Fattouh Jul 16 '20 at 12:44
-
1To combat unwanted pop-ups, browsers may not display prompts created in beforeunload event handlers unless the page has been interacted with, or may even not display them at all. SOURCE: https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event – Maher Fattouh Jul 16 '20 at 12:45
1 Answers
7
Many developers treat the unload event as a guaranteed callback and use it as an end-of-session signal to save state and send analytics data, but doing this is extremely unreliable, especially on mobile! The unload event does not fire in many typical unload situations, including closing a tab from the tab switcher on mobile or closing the browser app from the app switcher.
For this reason, it's always better to rely on the visibilitychange event to determine when a session ends, and consider the hidden state the last reliable time to save app and user data.
source: Page Lifecycle API

Quentin
- 914,110
- 126
- 1,211
- 1,335

mitchellcarroll
- 135
- 1
- 5