PhoneGap Back Button退出应用程序

时间:2014-10-28 06:07:40

标签: javascript html css cordova

目前我正在使用PhoneGap(Cordova 2.2),JQuery和Javascript开发移动应用程序。登陆页面是登录页面。因此,一旦我使用登录凭据进入仪表板页面,当我单击返回按钮时,其返回登录页面不会停留在仪表板页面上。我能做什么???建议欢迎。

我已经尝试了,

SCRIPT

<head>
<script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
<script type="text/javascript" charset="utf-8">

// Call onDeviceReady when Cordova is loaded.
//
// At this point, the document has loaded but cordova-2.2.0.js has not.
// When Cordova is loaded and talking with the native device,
// it will call the event `deviceready`.
//
function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

// Cordova is loaded and it is now safe to call Cordova methods
//
function onDeviceReady() {
    // Register the event listener
    document.addEventListener("backbutton", onBackKeyDown, false);
}

// Handle the back button
//
function onBackKeyDown() {
    alert("Back Button Clicked"); //called the alert..checking
}

</script>

HTML

<body onload="onLoad()">
</body>

我的onDeviceReady和onBackKeyDown函数无法正常工作/被解雇。我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

尝试使用loggin变量检查是否登录并在回调函数上执行某些操作。

function onBackKeyDown(evt) {
     evt.preventDefault();
     evt.stopPropagation();
     if(loggedin=='yes'){
         //dont do anything or show popup
     }else{
         history.back();
     }
}

您需要先绑定eventLinstener,然后在页面onLoad OR $(document).ready()方法中调用 app.initialize()

 var app = {
 // Application Constructor
   initialize: function() {
       this.bindEvents();
   },

   // Bind any events that are required on startup. Common events are:
   // 'load', 'deviceready', 'offline', and 'online'.
   bindEvents: function() {

      document.addEventListener('deviceready', this.onDeviceReady, false);

   },

   onDeviceReady: function() {
       document.addEventListener("backbutton", onBackKeyDown, false);
   }
};

答案 1 :(得分:0)

试试这个

    //after successful  login
        localStorage.setItem('login', 1);
// and your back function callback
        function onBackKeyDown(e) {
        e.preventDefault();
        e.stopPropagation();
        var isLogin = localStorage.getItem('login);
        if (isLogin){
        // stay here
        } else {
        history.back();

        }
           }