Quantcast
Channel: Ionic Forum - Latest posts
Viewing all articles
Browse latest Browse all 229600

Ionic and onbeforeunload

$
0
0

A slight tweak to the onbeforeunload implementation got it to work in Chrome. I also am using the onhashchange to try to detect other back button use. Full implementation is below for anyone else who is in a similar boat. Seems to be a reasonable result until the lazy-loading/routing/nav pieces are worked out and stable.

listenToWebEvents() {
  window.onbeforeunload = (e) => {
    this.loggingService.info("user navigating away from page");

    //If user is logged in, warn them before leaving
    if (this.auth.user) {
      var warnText = 'Leaving the site?  If not, please use the in-app navigation and not the back button.';
      e.returnValue = warnText;
      return warnText;
    }
  };

  window.onhashchange = (e) => {
    this.loggingService.info("user changing hash/url values");

    //If user is logged in, warn them since they may be using the back button here
    if (this.auth.user) {
      //Present an alert
      var navWarningAlert = this.alertCtrl.create({
        title: 'Navigation Warning',
        message: 'Using the back button?  If so, please use the in-app navigation and not the back button.',
        buttons: ['Dismiss']
      });
      navWarningAlert.present();
    }
  };
}

Viewing all articles
Browse latest Browse all 229600

Trending Articles