简体   繁体   中英

How to use history.go(-1) Javascript when used in alert box for a jQuery mobile app build via phonegap

Actually, I am trying to check for internet connection and if there is no internet connection available then I am popping an alert box showing Sorry, Can't display data and on user pressing the ok button, I want to transfer him back to the home page of my app.

My current Javascript function is as follows, but its not working as expected.

   function showerror(){
      alert("Sorry, Check your Internet Connection. Going Back to the Home page. showerror()");   
      history.go(-1);
      }

Please any kind of help is appreciated.

You should use the navigator.notification.alert method to show a device specific alert with a callback when the dialog is closed. Then use history.back() to send the user to the previous page.

http://docs.phonegap.com/en/1.8.1/cordova_notification_notification.md.html#notification.alert

Example:

function showerror() {
  navigator.notification.alert(
    'Sorry, Check your Internet Connection. Going Back to the Home page. showerror()',
    function() {
      history.back();
    }
  );
}

Edit:

If you know the specific page to return to you could always use $.mobile.changePage to go to that page. The only issue is when running on devices with a back button (Android, WP7).

Example:

function showerror() {
  navigator.notification.alert(
    'Sorry, Check your Internet Connection. Going Back to the Home page. showerror()',
    function() {
      $.mobile.changePage('#homePage');
    }
  );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM