最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery mobile - PhoneGap Javascript not working other than in index file - Stack Overflow

programmeradmin1浏览0评论

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=".0rc1/jquery.mobile-1.0rc1.min.css" />
        <link rel="stylesheet" href="docs/_assets/css/jqm-docs.css" />
        <script src=".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=".0rc1/jquery.mobile-1.0rc1.min.js"></script>
        <link rel="stylesheet" href="n_style.css" />

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


    </body>
</html>

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./mobile/1.0rc1/jquery.mobile-1.0rc1.min.css" />
        <link rel="stylesheet" href="docs/_assets/css/jqm-docs.css" />
        <script src="http://code.jquery./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./mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script>
        <link rel="stylesheet" href="n_style.css" />

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


    </body>
</html>
Share Improve this question edited Nov 2, 2011 at 18:12 Bakudan 19.5k9 gold badges55 silver badges75 bronze badges asked Nov 2, 2011 at 17:50 AtmaAtma 29.8k57 gold badges206 silver badges306 bronze badges 1
  • < is removed from all of the tags so as to render in stack overflow – Atma Commented Nov 2, 2011 at 17:59
Add a ment  | 

2 Answers 2

Reset to default 5

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

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

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.

发布评论

评论列表(0)

  1. 暂无评论