Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile browsers don't trigger `beforeunload`/`onload` when closing browser

I am working on mobile browser about 3 months and now i have a boring problem with beforeunload and onload. 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?

like image 736
sina zarei Avatar asked Oct 16 '22 02:10

sina zarei


1 Answers

"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

like image 193
mitchellcarroll Avatar answered Oct 19 '22 02:10

mitchellcarroll