Pharaz Fadaei
Pharaz Fadaei

Reputation: 1827

Phonegap doesn't show alert

why phonegap doesn't show alert notification ? (running on iPhone 5.1.1 , phonegap phonegap-2.1.0-0-g26d211b, mountain lion 10.8.2, xcode 4.4.1)

<html>
<head>
<link rel="stylesheet" href="themes/css/apple.css" title="jQTouch">
<script src="src/lib/zepto.min.js" type="text/javascript" charset="utf-8"></script>
<script src="cordova-2.1.0.js" type="text/javascript" charset="utf-8"></script>
<script src="src/jqtouch.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
    $(document).ready(function(){
     try
     {
        navigator.notification.alert('Hello', ok, 'Title', 'Button!');  
     }
     catch(e)
     {
        alert("doesn't support!!");
     }
    }
    function ok() {}
</script>
</head>
</html>

Upvotes: 3

Views: 6455

Answers (1)

Gajotres
Gajotres

Reputation: 57309

Because you are trying to show an alert before phonegap/cordova framework is loaded.

$(document).ready(function(){ is not going to help you here, you must use this event to check if phonegap is successfully loaded:

document.addEventListener("deviceReady", deviceReady, false);

function deviceReady() {
    // Now safe to use the PhoneGap API
}

More about this event: http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html

PhoneGap consists of two code bases: native and JavaScript. While the native code is loading, a custom loading image is displayed. However, JavaScript is only loaded once the DOM loads. This means your web application could, potentially, call a PhoneGap JavaScript function before it is loaded.

Upvotes: 8

Related Questions