Atma
Atma

Reputation: 29767

PhoneGap Javascript not working other than in index file

Hi I'm trying to run a javascript function (an alert to test) but it is not firing when in a page other than the index.html.

here is an example of a page that is not firing the javascript alert ( note that < is removed from all of the tags so as to render in stack overflow):

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
        <script type="text/javascript" charset="utf-8">
            document.addEventListener("deviceready", onDeviceReady, false);

            // PhoneGap is loaded and it is now safe to make calls PhoneGap methods
            //
            function onDeviceReady() {
            document.addEventListener("deviceready", onDeviceReady, false);
            }


            function onDeviceReady() {
            alert("test");
            navigator.notification.alert("PhoneGap is working");
            }

        </script>

        <link rel="stylesheet"  href="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.css" />
        <link rel="stylesheet" href="docs/_assets/css/jqm-docs.css" />
        <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
        <script src="experiments/themeswitcher/jquery.mobile.themeswitcher.js"></script>
        <script src="docs/_assets/js/jqm-docs.js"></script>
        <script src="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script>
        <link rel="stylesheet" href="n_style.css" />

    </head>
    <body onload="onBodyLoad()">


    </body>
</html>

Upvotes: 1

Views: 5034

Answers (2)

RyderAdam
RyderAdam

Reputation: 123

Your Javascript isn't running because you don't have an onBodyLoad function. I would wrap document.addEventListener("deviceready", onDeviceReady, false); in an onBodyLoad function. Also, if you are having javascript errors, try opening the page in firefox and using firebug to check for errors.

Upvotes: 0

Mike Robinson
Mike Robinson

Reputation: 25159

onDeviceReady only fires once when the app starts up. See documentation: http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html#deviceready

It's the function that indicates PhoneGap is fully loaded.

Upvotes: 5

Related Questions