【问题标题】:How to use history.go(-1) Javascript when used in alert box for a jQuery mobile app build via phonegap如何在通过 phonegap 构建 jQuery 移动应用程序的警报框中使用 history.go(-1) Javascript
【发布时间】:2012-06-24 09:25:18
【问题描述】:

实际上,我正在尝试检查互联网连接,如果没有可用的互联网连接,那么我会弹出一个警告框,显示对不起,无法显示数据,并且在用户按下确定按钮时,我想将他转回到我的应用程序的主页。

我当前的 Javascript 函数如下,但它没有按预期工作。

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

感谢任何形式的帮助。

【问题讨论】:

  • 你试过history.back()吗,不过这只会退回一级
  • 如果没有连接,为什么要将用户发送回您的主页?此外,如果您想将用户发送到特定页面,请执行以下操作:location.href = "http://yoursite.com/";

标签: javascript jquery cordova jquery-mobile dom-events


【解决方案1】:

您应该使用navigator.notification.alert 方法在对话框关闭时显示带有回调的设备特定警报。然后使用history.back() 将用户转到上一页。

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

例子:

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

编辑:

如果您知道要返回的特定页面,您可以随时使用$.mobile.changePage 转到该页面。唯一的问题是在带有后退按钮的设备(Android、WP7)上运行时。

例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    相关资源
    最近更新 更多