Phonegap设备就绪监听器

时间:2014-06-18 16:32:37

标签: cordova

我正在尝试使用phonegap自定义backButton操作。我的html索引文档的主体看起来如下。

<body onLoad="onLoadData()">

然后我尝试设置deviceready listener,然后设置backbutton监听器。在后退按钮上,代码永远不会到达设置的后退按钮。我想知道是否有人知道为什么这不起作用。

function onLoadData(){
  document.addEventListener("deviceready", onDeviceReady(), false);
  $.mobile.changePage("#login");
}
function onDeviceReady(){
    document.addEventListener("backbutton", backButtonClick, false);
}
function backButtonClick(){
    var activePage = $.mobile.activePage.attr('id');
    console.log("Reached here : " + activePage);
}

2 个答案:

答案 0 :(得分:0)

onDeviceReady将不会被调用,因为它在侦听器中的设置方式。如评论中所述,更改:

document.addEventListener("deviceready", onDeviceReady(), false);

为:

document.addEventListener("deviceready", onDeviceReady, false);

答案 1 :(得分:0)

您已正确添加cordova.js了! 如果是这样,那么你可以试试这个。在onLoadData方法之外的脚本标记之后添加事件侦听器。无需在onLoadData

中声明它
<script>


 document.addEventListener("deviceready", onDeviceReady, false);


function onDeviceReady(){
    document.addEventListener("backbutton", backButtonClick, false);

    $.mobile.changePage("#login");
}
function backButtonClick(){
    var activePage = $.mobile.activePage.attr('id');
    console.log("Reached here : " + activePage);
}


</script>

正如道森所提到的,请记得从onDeviceReady调用中删除()