2012-02-13 29 views
0

我想這個代碼中應用程序在iphone中成功加載應用程序時顯示警報。但是這不起作用。這是phonegap應用程序,這個應用程序是index.html。警告不起作用在手機中的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> 

回答

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

哪裏寫這段代碼?我試過但不工作。沒有警報消息正在顯示。 :( – 2012-02-13 10:38:31

+1

你的phonegap.js適用於iOS?試試這個https://github.com/davejohnson/phonegap-js/blob/master/src/ios/phonegap.js.base或http://piwigo.org/ dev/browser/extensions/piwigo_mobile/ios/www/phonegap.js – coppettim 2012-02-13 10:45:59

+0

Thnx :)第二個鏈接工作。這只是版本問題。現在我能夠在我的應用程序中顯示警報:) – 2012-02-14 08:53:14

1

這個代碼是根據我的版本和sorurce路徑工作在我的模擬器我只是改變js/phonegap-1.3.0.jsphonegap-1.2.0.js。所以請檢查您的phonegap js版本,並檢查它的路徑。我的js文件位於www文件夾中,其中放置了index.html

+0

這是爲我工作的Hello World項目。但我只是改變了index.html中的代碼來測試連接。當我更改index.html時,是否還有其他需要更改的內容? – 2012-02-13 10:36:46

+0

什麼是你的js版本和js文件的路徑? – 2012-02-13 10:39:42

+0

1.3.0。它位於www文件夾內的js文件夾中。 – 2012-02-13 10:43:27

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>