Phonegap - navigator.app.backHistory() not working on HTML back button

前端 未结 4 1579
灰色年华
灰色年华 2020-11-29 06:27

In my app i am using phonegap 2.6.For back button, I am using the following function

document.addEventListener(\"backbutton\", onBackKeyDown, false);

functi         


        
相关标签:
4条回答
  • 2020-11-29 06:29

    I have tried 3 separate things when I faced the same situation:

    • window.history.back()

    • navigator.app.backHistory();

    • History.go(-1);

    Individually, none of these solve the problem. I put all 3 things together and much to my surprise it worked. I really don't know what is behind it.

    Then I decreased to two functions and removed:

    • window.history.back()

    Now I am using this function and it is working fine.

    //Works Fine
    function onBackKeyDown() {
        history.go(-1);
        navigator.app.backHistory();
    }
    
    0 讨论(0)
  • 2020-11-29 06:31

    it depends where you are: on my windowsphone 8.1 lumia 925, it works history.go(-1);, while navigator.app.backHistory(); causes an exception before crashing.

    On my Android (I believe the majority), navigator.app.backHistory(); works properly.

    0 讨论(0)
  • 2020-11-29 06:43

    If you use the attribute data-rel="back" on an anchor, any clicks on that anchor will mimic the back button, going back one history entry and ignoring the anchor's default href.

    0 讨论(0)
  • 2020-11-29 06:50

    Solved! Stop getting "TypeError: navigator.app is undefined"

    A function I created that will first check what device you're on and then apply the relevant script:

    function onBackKeyDown() {
        var userAgent = navigator.userAgent || navigator.vendor || window.opera;
        if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/iPod/i)) {
            // IOS DEVICE
            history.go(-1);
        } else if (userAgent.match(/Android/i)) {
            // ANDROID DEVICE
            navigator.app.backHistory();
        } else {
            // EVERY OTHER DEVICE
            history.go(-1);
        }
    }
    

    Call function by adding this to your back link/button:

    onclick="onBackKeyDown()"
    
    0 讨论(0)
提交回复
热议问题