0

I have started to learn PhoneGap/Cordova to create app for mobile devices.

I created test app (cordova create) and added javascript file with simple function.

file test1.js:

function doalert() {
    alert("Test1 alert!");
}

Then I put button into generaten index.html + included mentioned js script file:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <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">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>rth Test1</title>

        <script type="text/javascript" src="js/test1.js"></script>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>

        <button type="button" onclick="doalert()">Alert</button>
    </body>
</html>

As you can see, button should invoke the doalert() function, which displays alert.

Here is the problem:
If I build this project into android application (apk) and install it on emulator or on my device, it doesn't work as expected. Application launches and shows "Device is ready". But, tapping on button doesn't do anything (looks like the JavaScript function is not called).

If I run it from browser (Firefox) or if I use PhoneGap server - client applications, then everything works fine.

I have checked inside of apk, and all files seems to be in place.

Using below software versions:

Android: 4.4.4
Cordova: 4.0.0
Rao
  • 20,781
  • 11
  • 57
  • 77
  • Put the button tag above the two scripts – Ray Feb 27 '16 at 17:54
  • Unfortunately that didn't help. I tried several combinations of placement of button and tag and test1.js script tag. Nothing was successful. – Roman Theuer Feb 29 '16 at 07:24
  • Ok then delete all of the generated HTML that Cordova did for you and write your own. – Ray Feb 29 '16 at 12:20
  • I have made it work. However in some strange way. Instead of installing and using Cordova CLI (it is marked outdated on PhoneGap pages) I have installed PhoneGap CLI directly (http://docs.phonegap.com/references/phonegap-cli/install/). If app is built directly using PhoneGap CLI, it works ok. – Roman Theuer Feb 29 '16 at 13:35

0 Answers0