当前位置: 动力学知识库 > 问答 > 编程问答 >

android - "[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

网友答案:

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);
分享给朋友:
您可能感兴趣的文章:
随机阅读: