2

I'm working on a hybrid app with Cordova but my little experience with JavaScript doesn't let me handle this.

I created a Cordova plugin which includes the plugin.js file

var exec = require('cordova/exec');

var PLUGIN_NAME = 'MyCordovaPlugin';

var MyCordovaPlugin = {
  echo: function(phrase, cb) {
    exec(cb, null, PLUGIN_NAME, 'echo', [phrase]);
  },
  getDate: function(cb) {
    exec(cb, null, PLUGIN_NAME, 'getDate', []);
  }
};

module.exports = MyCordovaPlugin;

Since it's been exported, I'm trying to use MyCordovaPlugin variable in my index.html

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript View</title>
    <script type="text/javascript">
        function createTransaction(){
            window.MyCordovaPlugin.echo('echo', function() { 

            });
            return false;
        }
        window.onload = function(){
            var form = document.getElementById("form");
            form.onsubmit = createTransaction;
        }
    </script>
</head>
    <body>
        <form id="form">
            <input type="submit" value="Create transaction" />
        </form>
    </body>
</html>

How can index.html have access to the variable?

1 Answer 1

1

To make Cordova plugins accessible you have to import the cordova.js in your index.html:

<script type="text/javascript" src="cordova.js"></script>
Sign up to request clarification or add additional context in comments.

3 Comments

omg! Thank you so much! I spent hours trying to figure out what could be wrong. Can you tell me where did you get that reference? I could not find a solution
I can't find where it's explained, but cordova.js contains the js needed for Cordova apps to work
How to access variables from this js file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.