在html中调用phonegap / cordova插件功能

时间:2016-12-27 22:50:41

标签: javascript cordova phonegap-plugins cordova-plugins

我对javascript很新,我开始用Phonegap做一些练习。

我试图在html中调用插件函数,但是如果我在一个按钮内调用它就可以正常工作:

<button onclick="cordova.plugins.autoStart.disable()">Stop AutoStart</button>

如果我尝试直接调用它,我会得到:&#34; autoStart未定义&#34;在控制台中。

<script type="text/javascript">
    cordova.plugins.autoStart.enable();
</script>

如何在没有按钮的情况下将其调用为html?

UPDATE1:

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="msapplication-tap-highlight" content="no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
    <meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *" />

     <link rel="stylesheet" href="css/styles.css">
     <link rel="stylesheet" href="css/jquery-ui.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/jquery-1.11.2.min.js"><\/script>')</script>        
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

    <script type="text/javascript" src="cordova.js"></script>
</head>
<body>

    <button onclick="cordova.plugins.autoStart.disable()">Stop AutoStart</button>
    <div class="perspective">
        <img id="box1"/>
        <img id="box2"/>
    </div>

    <p id="date"></p>
    <p id="time"></p>

    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
        jQuery(document).ready(function () {
            //THE ERROR IS HERE
            cordova.plugins.autoStart.enable();
        });
    </script>        
</body>
</html>

1 个答案:

答案 0 :(得分:0)

当你创建cordova项目时,无论你是通过命令行界面还是Phonegap桌面创建的,都应该创建一个app.js文件。

插件只能在设备准备好后使用,因此您应该将代码放在app.js中的onDeviceReady()函数内。