2

有没有办法将后退按钮(设备后退按钮)作为返回页面的默认功能来处理?我需要在后退按钮上实现相同的功能转到上一页。如果没有前一页(第一页),则退出应用程序。这在PhoneGap中可能吗?
我还需要在推送另一个页面之前弹出页面,这在 jQuery 中是否可行?

4

1 回答 1

1

Checking window.location.length would be the easiest way to determine if you're on the first page, but this isn't available in Phonegap.

But since you're using JQM, you can either use the navigate event as Omar suggests or could manually count the number of pages shown and the number of pages gone back (same thing) and use this to determine if the first page is being shown and whether to exit the app. Something like this would work:

var pageHistoryCount = 0;
var goingBack = false;

$(document).bind("pageshow", function(e, data) {
    if (goingBack) {
        goingBack = false;
    } else {
        pageHistoryCount++;
        console.log("Showing page #"+pageHistoryCount);
    }
});

function exitApp() {
    console.log("Exiting app");
    navigator.app.exitApp();
}

function onPressBack(e) {
    e.preventDefault();
    if(pageHistoryCount > 0) pageHistoryCount--;
    if (pageHistoryCount == 0) {

        navigator.notification.confirm("Are you sure you want to quit?", function(result){
            if(result == 2){
                exitApp();
            }else{
                pageHistoryCount++;
            }
        }, 'Quit My App', 'Cancel,Ok');
    } else {
        goingBack = true;
        console.log("Going back to page #"+pageHistoryCount);
        window.history.back();
    }
}

function deviceready() {
    $(document).bind('backbutton', onPressBack);
}
$(document).bind('deviceready', deviceready);

As for the second part of your question:

Secondly i need to pop page before going to push another page is this posible in jquery ?

It's not clear what you're asking here. Do you mean you want to show some kind of popup content like a dialog between every page change? Please clarify then I can help you :-)

于 2013-06-23T15:21:56.937 回答