简体   繁体   English

Javascript后退按钮在Cordova应用程序上不起作用

[英]Javascript back button not working on Cordova app

I'm using this code here to as router to handle pages in single page application ( Cordova ). 我在这里使用此代码作为路由器来处理单页应用程序(Cordova)中的页面。

var router = (function () {
    "use strict";
    var routes = [];
    function addRoute(route, handler) {
        routes.push({parts: route.split('/'), handler: handler});
    }
    function load(route) {
        window.location.hash = route;
    }
    function start() {
        var path = window.location.hash.substr(1),
            parts = path.split('/'),
            partsLength = parts.length;

        for (var i = 0; i < routes.length; i++) {
            var route = routes[i];
            if (route.parts.length === partsLength) {
                var params = [];
                for (var j = 0; j < partsLength; j++) {
                    if (route.parts[j].substr(0, 1) === ':') {
                        params.push(parts[j]);
                    } else if (route.parts[j] !== parts[j]) {
                        break;
                    }
                }
                if (j === partsLength) {
                    route.handler.apply(undefined, params);
                    return;
                }
            }
        }
    }
    $(window).on('hashchange', start);
    return {
        addRoute: addRoute,
        load: load,
        start: start
    };
}());

and this router is used like this: 该路由器的用法如下:

router.addRoute('', function() {
    document.getElementById("bodyDiv").innerHTML = homePageHtml;
});
router.addRoute('page1', function() {
    document.getElementById("bodyDiv").innerHTML = page1Html;
});

i want to add back button to my application but it didn't work. 我想将后退按钮添加到我的应用程序,但是没有用。 i did try 我确实尝试过

window.history.back(); window.history.back();

history.go(-1); history.go(-1);

navigator.app.backHistory(); navigator.app.backHistory();

but none of them worked the way it suppose to work. 但他们都没有按照预期的方式运作。 when i open the application on google chrome, it working just fine but when i convert it to mobile application using Cordova the back button doesn't work as expected sometimes the back button instead of taking you to the previous page it refresh the current one. 当我在google chrome上打开应用程序时,它工作正常,但是当我使用Cordova将其转换为移动应用程序时,后退按钮将无法正常工作,有时后退按钮而不是将您带到上一页会刷新当前页面。

You can use this: 您可以使用此:

This is only an example for backbutton press. 这仅是后按按钮的示例。

document.addEventListener("backbutton", function () {       
        exitAppPopup();        
}, false);

function exitAppPopup() {
    navigator.app.exitApp();
}

Do you have tried something like this? 您是否尝试过类似的方法?

document.addEventListener("backbutton", back_function, false);

function back_function() {
    // Handle event
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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