ondeviceready和backbutton在phonegap android中不起作用?

时间:2014-05-06 11:18:14

标签: android cordova

    document.addEventListener("ondeviceready", function(){ alert("123");},true);
    document.addEventListener("backbutton", function(){ alert("123");},true);

//我也单独使用了功能但没有工作   //我正在使用正确的cordova库    //我把这段代码保存在外部js文件中,然后它也没有用。   //我还在将任何其他js文件包含到HTML中之前调用它   //同样的事情也发生在' backbutton'事件

  <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/common.css" />
        <link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.4.2.min.css.css" />
        <title>Since your car is manufactured before 2000</title>
         <script type="text/javascript" src="cordova-2.4.0.js"></script>
         <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
        <script type="text/javascript" src="js/jquery.mobile-1.4.2.js"></script>
        <script type="text/javascript">
           document.addEventListener("backbutton", function(){
         alert("Back button pressed");
        },true);
    </script>
      </head>

    <body>

1 个答案:

答案 0 :(得分:0)

该事件称为 deviceready ,而不是 ondeviceready

此外,您需要在 deviceready -event触发之后注册 backbutton 事件,否则,它将无效。

来自Cordova文档的修改示例:

<!DOCTYPE html>
<html>
  <head>
    <title>Device Ready Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    // device APIs are available
    //
    function onDeviceReady() {
        document.addEventListener("backbutton", onBackButton, false);
    }

    function onBackButton(){
      // your code here
    }

    </script>
  </head>
  <body onload="onLoad()">
  </body>
</html>