简体   繁体   中英

Call phonegap/cordova plugin function within html

I'm very new to javascript and I started to make some practice with Phonegap.

I'm trying to call a plugin function within html but if I call it within a button it works fine:

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

while if I try to call it directly I get: "autoStart is not defined" in the console.

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

How can I call it into html without the button?

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>

When you created your cordova project, whether you did so via Command Line Interface or Phonegap Desktop, an app.js file should have been created as well.

Plugins can only be used after the device is ready, so you should put your code inside of the onDeviceReady() function inside of app.js.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM