0

What are an alternatives to beforeunload event on mobile devices? According to the docs (https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event) beforeunload event doesn't work on mobile devices.

Our customer wants a popup message before closing the browser if user don't save the edited form.

On browsers it works fine with 'beforeunload' event.

Angular code:

  @HostListener('window:beforeunload', ['$event'])
  unloadNotification($event: BeforeUnloadEvent) {
    if (!this.hasFormChanged) return;
    $event.returnValue();
  }

Thanks to everyone for your help.

devZ
  • 606
  • 1
  • 7
  • 23
  • I don't think there is any solution to your issue. As explained in the docs, closing the app from the apps manager will siply close the app, no matter what. – MGX Jan 03 '23 at 10:02
  • Take a look at [this answer](https://stackoverflow.com/a/64360530/10304804). -- Mobiles are not triggering the beforeunload in every case. – Christopher Jan 03 '23 at 10:03
  • But are there any alternatives so I can detect when user closes the browser in mobile phone with javascript? – devZ Jan 03 '23 at 10:53
  • There is no real alternative that can be used on mobiles. Websites get automatically unloaded and/or paused if the user just presses the home button or switches to another tab. - The [`visibilitychange`](https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilitychange_event) can help to detect if the window is still displayed at a mobile device, but that's it. – Christopher Jan 03 '23 at 11:35
  • Thanks for your answer. I will implement visibilitychange and check how it works. – devZ Jan 03 '23 at 12:06

0 Answers0