【问题标题】:Phonegap - How to call one javascript function in another file from index page?Phonegap - 如何从索引页面调用另一个文件中的一个 javascript 函数?
【发布时间】:2012-03-01 15:37:31
【问题描述】:

我是 iOS 和 PhoneGap 的新手。我有 index.html 文件和一个名为 MyClass.js 的 javascript 文件。

在我的 MyClass.js 中,我有一个函数 -

var MyClass = {

testfunc: function() {
    navigator.notification.alert("Success : \r\n");
}

}

我正在尝试从 index.html 函数中调用它,例如 -

MyClass.testfunc();

在我的 Phonegap.plist 文件中,我有一个条目 MyClass-Myclass 作为字符串类型的键值对。但是我没有收到警报。我究竟做错了什么?

【问题讨论】:

  • 您在日志中遇到的错误是什么?

标签: javascript iphone ios cordova phonegap-plugins


【解决方案1】:

您是否在 index.html 中包含以下内容:

<script src="MyClass.js"></script>

这将允许您在 index.html 文件中使用 MyClass.js 函数

【讨论】:

  • 我做到了,但仍然无法调用外部函数:(
【解决方案2】:

您的警报标记错误...

navigator.notification.alert(
    'Some Alert Text here', // alert message
    successCallback, // callback function
    'Alert Title, // alert title
    'OK' // button text
);

【讨论】:

  • 并非如此。只是“成功:\r\n”也应该可以工作。当我将它放在脚本标签内的 index.html 文件中时,它可以工作。
  • 我在 onDeviceReady() 中调用它
  • 超级奇怪。你加载你的js文件的顺序是什么?在你的 phonegap js 之前还是之后?
  • 负载是什么意思?我在我自己的文件之前指定
  • 这正是我的意思,很奇怪。有时你加载脚本的顺序会影响它们,但你做对了。我不完全确定......你有什么错误吗?
【解决方案3】:

你好,你可以试试下面的代码:

// MyClass.js
var MyClass = {
    testFunction: function() {
        alert("hi there");
    }
};

// index.html
<html>
<head>
</head>
<body>
    <script src="MyClass.js"></script>
    <script src="phonegap-1.3.0.js"></script>
    <script type="text/javascript">
        document.addEventListener("deviceready", function() {
            navigator.notification.alert("hi there \r\n");
            //alert("hi there \r\n");
        });
    </script>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    相关资源
    最近更新 更多