2013-10-11 61 views
1

我想使用phonegap,但由於某種原因,我的JavaScript沒有運行。什麼到目前爲止,我已經試過是:Phonegap not running javascript

<html> 
<head> 
    <meta 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/index.css" /> 
    <title>Hello World</title> 

    <script type="text/javascript" src="phonegap.js"></script> 
    <script type="text/javascript" charset="utf-8"> 
     document.addEventListner("deviceready", onDeviceReady, false); 

     function onDeviceReady() { 
      alert('hey'); 
      document.addEventListener("online", onOnline, false); 
      document.addEventListener("offline", onOffline, false); 
     } 

     function onOnline() { 
      alert('device is online'); 
     } 

     function onOffline() { 
      alert('device is offline'); 
     } 
    </script> 
</head> 
<body> 
    <div class="app"> 
     <h1>PhoneGap</h1> 
     <div id="deviceready" class="blink"> 
      <p class="event listening">Connecting to Device</p> 
      <p class="event received">Device is Ready</p> 
     </div> 
    </div> 
</body> 

,但由於某種原因,我不能得到一個警報。我使用的PhoneGap 2.9.0在IOS 7

什麼我也tryed是:

<html> 
<head> 
    <meta 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/index.css" /> 
    <title>Hello World</title> 

    <script type="text/javascript" src="phonegap.js"></script> 
    <script type="text/javascript" charset="utf-8"> 
     document.addEventListener("deviceready", onDeviceReady, false); 

// PhoneGap is ready 
// 
function onDeviceReady() { 
    // Empty 
} 

// alert dialog dismissed 
function alertDismissed() { 
    // do something 
} 

// Show a custom alert 
// 
function showAlert() { 
    navigator.notification.alert(
     'You are the winner!', // message 
     alertDismissed,   // callback 
     'Game Over',   // title 
     'Done'     // buttonName 
    ); 
} 
    </script> 
</head> 
<body> 
    <div class="app"> 
     <h1>PhoneGap</h1> 
     <div id="deviceready" class="blink"> 
      <p class="event listening">Connecting to Device</p> 
      <p class="event received">Device is Ready</p> 
      <p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p> 
     </div> 
    </div> 
</body> 

+0

iOS7的JavaScript忽略 「警報()」 呼叫... FYI – opalenzuela

+0

一些瀏覽器不顯示'alert'。試試'console.log'而不是 – CodingIntrigue

+0

我需要使用什麼而不是? – Ajeo

回答

2
 document.addEventListener("online", onOnline, false); 
     document.addEventListener("offline", onOffline, false); 

這些功能將在建立新的互聯網連接和禁用網絡連接只

工作

當您打開應用程序時,它不起作用。這些事件監聽器的「在線」和「離線」主要用於跟蹤互聯網連接是否可用。

而且你不應該使用它可能無法在IOS 7.使用通知類警報

+0

但爲什麼我沒有看到onDeviceReady()的警報? – Ajeo

+0

我已編輯我的答案檢查它 –

+0

檢查控制檯中的錯誤xcode –

0

支持PhoneGap的警報功能試試這個它在我結束工作,可能是你忘了添加cordova.js

<html> 
<head> 
    <meta 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/index.css" /> 
     <script type="text/javascript" src="cordova.js"></script> 
    <title>Hello World</title> 
    <script type="text/javascript" src="phonegap.js"></script> 
    <script type="text/javascript" charset="utf-8"> 
     document.addEventListener("deviceready", onDeviceReady, false); 

     // PhoneGap is ready 
     function onDeviceReady() { 
      // Empty 
     } 

    // alert dialog dismissed 
    function alertDismissed() { 
     // do something 
    } 

    // Show a custom alert 
    function showAlert() { 
     navigator.notification.alert(
            'You are the winner!', // message 
            alertDismissed,   // callback 
            'Game Over',   // title 
            'Done'     // buttonName 
            ); 
    } 
    </script> 
</head> 
<body> 
    <div class="app"> 
     <h1>PhoneGap</h1> 
     <div id="deviceready" class="blink"> 
      <p class="event listening">Connecting to Device</p> 
      <p class="event received">Device is Ready</p> 
      <p><a href="#" onclick="showAlert();">Show Alert</a></p> 
     </div> 
    </div> 
</body> 

+0

這取決於使用哪個版本的phonegap。使用phonegap 3+時,不應手動包含cordova.js或phonegap.js,包含的內容會在您構建項目時自動添加。你不應該包含cordova.js和phonegap.js,因爲phonegap.js會加載cordova.js – QuickFix