user1324068
user1324068

Reputation: 267

"[undefined] is not a function" when handling backbutton event in PhoneGap

I am trying to work with the back button with help from the PhoneGap Documentation and Override Android Backbutton behavior only works on the first page with PhoneGap.

My code:

<script>
document.addEventListener("deviceready", onDeviceReady, false);
  function onDeviceReady() {
    alert("df");
        console.log("PhoneGap Ready!");
         document.addEventListener("backbutton", handleBackButton, false);
    }

    function handleBackButton() {
        console.log("Back Button Pressed!");
        alert("df");

    }
</script>

But I get this error:

05-21 16:00:03.248: E/Web Console(1615): TypeError: Result of expression 'PhoneGap.fireDocumentEvent' [undefined] is not a function. at undefined:1

Upvotes: 2

Views: 7899

Answers (1)

davids
davids

Reputation: 6371

Check that you are linking the correct phonegap-x.js for the platform, the javascript code is different for android, iOS, etc.

When the back button is pressed in Android, the event 'backbutton' is fired, so if you want to go back in the navigation history, what you should do is attaching the following handler to it:

document.addEventListener("backbutton", function(e){
    e.preventDefault();
    navigator.app.backHistory();
}, true);

Upvotes: 3

Related Questions