On Back按钮事件显示确认是,使用Cordova InAppBrowser关闭window.open应用程序之前没有选项

时间:2016-04-18 12:15:12

标签: android jquery cordova phonegap-build hybrid-mobile-app

我是新手&陷入困境。这可能也有简单的解决方案..

我使用window.open使用InAppBrowser加载页面,而用户按下硬件后退按钮我想在应用关闭之前显示确认消息。我尝试了一些代码...... 这是代码

index.html

$(document).ready(function() {
    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {

       var ref = window.open('http://stackoverflow.com/', '_blank', 'location=no,hardwareback=yes');
       ref.addEventListener('loadstart', function(event) { SpinnerPlugin.activityStart("Initializing..."); });
       ref.addEventListener('loadstop', function(event) { SpinnerPlugin.activityStop(); });
       ref.addEventListener('loaderror', function(event) { alert('error: ' + event.message); SpinnerPlugin.activityStop();});
       ref.addEventListener('exit', function(event) {
        alert('Exit')
        });
       ref.addEventListener("backbutton", function () { //But This is not triggering...Don't know why..
         onConfirm();
      })
       function onConfirm(button) {
            if (button == 1){ //Yes button pressed...
                navigator.app.exitApp();
            } else{
              return false;
            }
        }
    }
});

即使我尝试了Cordova的backbutton事件......

document.addEventListener("backbutton", onBackKeyDown, false); //Listen to the User clicking on the back button
function onBackKeyDown(e) {
    navigator.notification.confirm("Are you sure you want to exit ?", onConfirm, "Confirmation", "Yes,No");

}

在关闭内部窗口后它会被正确触发.. 简而言之,我想在关闭内部窗口之前显示相同的确认框。

注意:我使用的是Cordova 6.1.0

非常感谢a-ton

1 个答案:

答案 0 :(得分:0)

我试过这个,它会工作,

document.addEventListener("backbutton", function(e){
        navigator.notification.confirm("Are you sure you want to exit the application?",fnLogout,"Warning","Ok,Cancel");
    }, false);



function fnLogout(button) {
    if(button == 1) {
        navigator.app.exitApp();
    } else {
        return;
    }                     
 }

修改 我试过你的代码,遇到同样的问题,

document.addEventListener("deviceready", onDeviceReady, false);
 document.addEventListener("backbutton", function(e){
        navigator.notification.confirm("Are you sure you want to exit the application?",fnLogout,"Warning","Ok,Cancel");
    }, false); 
  function onDeviceReady(){

       var ref = window.open('http://stackoverflow.com/', '_blank', 'location=no, hardwareback=yes');
       ref.addEventListener('loadstart', function(event) { SpinnerPlugin.activityStart("Initializing..."); });
       ref.addEventListener('loadstop', function(event) { SpinnerPlugin.activityStop();  });
       ref.addEventListener('loaderror', function(event) { alert('error: ' + event.message); SpinnerPlugin.activityStop();});
        ref.addEventListener('exit', function(event) { // Not Triggered
        alert('Exit');
        });  
         ref.addEventListener("backbutton", function(event){  // NOT triggered
           alert('Exit');
        navigator.notification.confirm("Are you sure you want to exit the application?",fnLogout,"Warning","Ok,Cancel");
    }, false);  

};
function fnLogout(button) {
    if(button == 1) {
        navigator.app.exitApp();
    } else {
        return;
    }                     
 }

很有可能无法在PhoneGap中覆盖InAppBrowser的后退按钮。

尝试在以下链接中获取简要信息: https://cordovablogsblogs.wordpress.com/2016/02/04/handle-android-back-button-on-phonegap-inappbrowser/

感谢。