警告不能在phonegap的html中工作

时间:2012-02-13 10:14:12

标签: iphone cordova alert

我正在尝试使用iphone中成功加载应用程序后应显示警报的代码。但这不起作用。这是phonegap应用程序,此应用程序是index.html。

<!DOCTYPE html>
<html>
    <head>
        <title>navigator.network.connection.type Example</title>

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

            // Wait for PhoneGap to load
            // 
            document.addEventListener("deviceready", onDeviceReady, false);

            // PhoneGap is loaded and it is now safe to make calls PhoneGap methods
            //
            function onDeviceReady() {
            checkConnection();
            }

            function checkConnection() {
                var networkState = navigator.network.connection.type;

                var states = {};
                states[Connection.UNKNOWN]  = 'Unknown connection';
                states[Connection.ETHERNET] = 'Ethernet connection';
                states[Connection.WIFI]     = 'WiFi connection';
                states[Connection.CELL_2G]  = 'Cell 2G connection';
                states[Connection.CELL_3G]  = 'Cell 3G connection';
                states[Connection.CELL_4G]  = 'Cell 4G connection';
                states[Connection.NONE]     = 'No network connection';

                alert('Connection type: ' + states[networkState]);
                //navigator.notification.alert('Connection type: ' + states[networkState]);
            }

            </script>
    </head>
    <body >
        <p>A dialog box will report the network state.</p>
          <a href="javascript:checkConnection()">Click</a>
    </body>
</html>

3 个答案:

答案 0 :(得分:2)

您是否正确设置了PhoneGap应用?我注意到你错过了<body onload="init();" >

下面的代码应该可以使用,我只是为你输入但是没有编译:-P

<!DOCTYPE HTML>
<html>
<head>
<title>navigator.network.connection.type Example</title>

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


            function init() {
              document.addEventListener("deviceready", onDeviceReady, false);
            }

            function checkConnection() {
                var networkState = navigator.network.connection.type;

                var states = {};
                states[Connection.UNKNOWN]  = 'Unknown connection';
                states[Connection.ETHERNET] = 'Ethernet connection';
                states[Connection.WIFI]     = 'WiFi connection';
                states[Connection.CELL_2G]  = 'Cell 2G connection';
                states[Connection.CELL_3G]  = 'Cell 3G connection';
                states[Connection.CELL_4G]  = 'Cell 4G connection';
                states[Connection.NONE]     = 'No network connection';

                alert('Connection type: ' + states[networkState]);
                //navigator.notification.alert('Connection type: ' + states[networkState]);
            }

</script>
</head>
<body onload="init();>
    <h1>Check connection</h1>
    <p>A dialog box will report the network state.</p>
          <a href="javascript:checkConnection()">Click</a>
    </body>
</html>

答案 1 :(得分:1)

此代码正在我的模拟器上工作我根据我的版本和sorurce路径将js/phonegap-1.3.0.js更改为phonegap-1.2.0.js。所以检查你的phonegap js版本,并检查它的路径。我的js文件位于www文件夹中,其中放置了index.html

答案 2 :(得分:1)

navigator.notification.alert(
            'You are the winner!',  // message
            alertDismissed,         // callback
            'Game Over',            // title
            'Done'                  // buttonName
        );