2013-01-12 54 views
0

我試着將下面的代碼放到html中並運行它,然後我將它上載到我的服務器中,並在我的iPhone中的Safari瀏覽器中打開鏈接,並單擊顯示確認並且沒有彈出窗口!有人可以請幫助。Phonegap確認提示

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Notification Example</title> 
    <script type="text/javascript" charset="utf-8" src="http://mobile-web-development-with-phonegap.eclipselabs.org.codespot.com/svn-history/r99/trunk/com.mds.apg/resources/phonegap/js/phonegap-1.0.0.js"></script> 
    <script type="text/javascript" charset="utf-8"> 
     // Wait for PhoneGap to load 
     document.addEventListener("deviceready", onDeviceReady, false); 
     // PhoneGap is ready   
     function onDeviceReady() { 
      // Empty 
     } 
     // process the confirmation dialog result 
     function onConfirm(button) { 
      alert('You selected button ' + button); 
     } 
     // Show a custom confirmation dialog 
       function showConfirm() { 
      navigator.notification.confirm(
      'You are the winner!', // message 
      onConfirm,    // callback to invoke with index of button pressed 
      'Game Over',   // title 
      'Restart,Exit'   // buttonLabels 
     ); 
     } 
    </script> 
    </head> 
    <body> 
    <p><a href="#" onclick="showConfirm(); return false;">Show Confirm</a></p> 
    </body> 
</html> 
+1

您正試圖從瀏覽器使用Phonegap API。如果您使用Phonegap製作本機應用程序,它們纔會有效。確保你瞭解Phonegap是什麼。 – keune

回答

2

您正在使用的代碼(navigator.notification.confirm)是專門針對移動平臺,是指到PhoneGap的移動應用程序中運行。如果您想在瀏覽器中將對話/確認消息編譯到應用程序之前進行測試,我會建議使用混合方法來檢測應用程序的環境,並使用本機confirm(message)或PhoneGap特定的Notification API。下面是一個對象,一直在爲我工作:

/** 
* The object encapsulates messaging functionality to work both in PhoneGap and 
* browser environment. 
* @author Zorayr Khalapyan 
* 
*/ 
var MessageDialogController = (function() { 

    var that = {}; 

    /** 
    * Invokes the method 'fun' if it is a valid function. In case the function 
    * method is null, or undefined then the error will be silently ignored. 
    * 
    * @param fun the name of the function to be invoked. 
    * @param args the arguments to pass to the callback function. 
    */ 
    var invoke = function(fun, args) { 
     if(fun && typeof fun === 'function') { 
      fun(args); 
     } 
    }; 

    that.showMessage = function(message, callback, title, buttonName) { 

     title = title || "DEFAULT_TITLE"; 
     buttonName = buttonName || 'OK'; 

     if(navigator.notification && navigator.notification.alert) { 

      navigator.notification.alert(
       message, // message 
       callback, // callback 
       title,  // title 
       buttonName // buttonName 
      ); 

     } else { 

      alert(message); 
      invoke(callback); 
     } 

    }; 

    that.showConfirm = function(message, callback, buttonLabels, title) { 

     //Set default values if not specified by the user. 
     buttonLabels = buttonLabels || 'OK,Cancel'; 
     var buttonList = buttonLabels.split(','); 

     title = title || "DEFAULT TITLE"; 

     //Use Cordova version of the confirm box if possible. 
     if (navigator.notification && navigator.notification.confirm) { 

       var _callback = function (index) { 
        if (callback) { 
         //The ordering of the buttons are different on iOS vs. Android. 
         if(navigator.userAgent.match(/(iPhone|iPod|iPad)/)) { 
          index = buttonList.length - index; 
         } 
         callback(index == 1); 
        } 
       }; 

       navigator.notification.confirm(
        message,  // message 
        _callback, // callback 
        title,  // title 
        buttonLabels // buttonName 
       ); 

     //Default to the usual JS confirm method. 
     } else { 
      invoke(callback, confirm(message)); 
     } 

    }; 

    return that; 

})(); 

希望這有助於!如果您有任何問題,請告訴我。

+0

我在哪裏插入代碼?我使用Phonegap html可以上傳示例模板文件,以便我可以測試和學習。 – Ben

+0

將代碼放入JavaScript文件中,並通過