Ajeo
Ajeo

Reputation: 73

Phonegap not running javascript

I am trying to use phonegap but for some reason is my javascript not running. What i have tried so far is:

<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>

but for some reason am i not getting a alert. I am using phonegap 2.9.0 on IOS 7

what i also tryed is:

<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>

Upvotes: 1

Views: 1120

Answers (2)

Gopal vaid
Gopal vaid

Reputation: 376

Try this its working at my end and might be you forgot to added 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>

Upvotes: 0

Manu M
Manu M

Reputation: 1064

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

These functions will work on establishing a new internet connection and disabling network connection only

It won't work when you open the app. These events listeners 'online' and 'offline' are mainly used to track if an internet connection is available.

Also you should not use alert function which might not be supported in ios 7. Use notification class in phonegap for alerts

Upvotes: 2

Related Questions