PhoneGap Javascript在索引文件中不起作用

时间:2011-11-02 17:50:48

标签: javascript jquery-mobile cordova

您好我正在尝试运行javascript函数(要测试的警报)但是在index.html以外的页面中没有触发。

这是一个未触发javascript警报的页面示例(请注意,<已从所有标记中删除,以便在堆栈溢出中呈现):

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

2 个答案:

答案 0 :(得分:5)

onDeviceReady仅在应用启动时触发一次。见文档: http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html#deviceready

这是表示PhoneGap已满载的功能。

答案 1 :(得分:0)

您的Javascript未运行,因为您没有onBodyLoad功能。我会将document.addEventListener("deviceready", onDeviceReady, false);包裹在onBodyLoad函数中。此外,如果您有javascript错误,请尝试在firefox中打开该页面并使用firebug检查错误。