我遇到了无法解决的问题。
我正在尝试操作 WindowsPhone 的后退按钮,但似乎不起作用
我尝试重用一个事件(实际上适用于 Android 应用程序),但它更像这样
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e) {
var location = window.location.href;
console.log(location);
var partial_location = location.split("#");
if (partial_location[1] == "menu/" || partial_location[1] == "login/") {
e.preventDefault();
navigator.app.exitApp();
} else {
navigator.app.backHistory();
}
}
由于它似乎不适用于 Windows Phone,经过一些研究,我通过 nuget WinJS 安装并尝试调用这样的函数
if (device.platform == "windows") {
WinJS.Application.onbackclick = function(evt){
onBackKeyDown(evt);
return true;
}
} else {
document.addEventListener("backbutton", onBackKeyDown, false);
}
我在 deviceready 事件中调用此事件,因此我没有解决方案。
它似乎没有检测到事件甚至函数。所以我想知道我做错了什么。
有什么建议吗?
最佳答案
希望您已经解决了问题,如果没有解决,您可以尝试以下方法。
您需要在 Cordova 项目的 index.js 文件中包含 backbutton() 函数。
您可以实现类似于下面的内容,
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind event listeners for 'deviceready' and 'backbutton'
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
// Here define your back button listener
document.addEventListener('backbutton', this.handleBackButton, false);
// If you need to support native back button you can do like this
// document.addEventListener('backbutton', this.handleBackButton, true);
},
// deviceready Event Handler
onDeviceReady: function () {
//OnDeviceReady function implementation
},
// backbutton Event handler
// Here define your logics
handleBackButton: function () {
// Check if you are in home page
// You need to check according to your application. For example if the home page (data page) is index-in, show alert message
if ($('.page-on-center').data('page') == "index-in") {
myApp.confirm('Are you sure you want to exit ?', 'Exit',
function () {
// If Click EXIT or Confirm
// This is not work for me
//navigator.navigation.exitApp();
// This works fine for me
navigator.app.exitApp();
},
function () {
// If click cancel do nothing
}
);
} else {
// Go to previous page
mainView.router.back();
}
},
// do something other than the default, like undo an operation,
// or step backwards through a multi-step operation
};
关于jquery - Cordova/Phonegap 应用程序上的 Windows Phone 中的后退按钮出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31340632/