Software Archive
Read-only legacy content
17061 Discussions

JavaScript not working on Android 4.4+

Peter_S_
Beginner
1,372 Views

Hi, I builded a simple test app for Android with XDK and it works perfectly in the emulator in Android 4.0, 4.2, 4.3, but the JavaScript code does not work neither in Android 4.4, nor 5.0, 5.1, 6.0, 7.0. Could someone help me to understand what is wrong ? Thanks. Here is the code:

<body>

<script>
        function getText() {
        document.getElementById('replace_text').innerHTML = 'A simple line';
    }
 </script>

<a href="#" onclick="getText()">Click here to replace text.</a>
    <br />
<div id="replace_text">Here is the text to replace.</div>

</body>

0 Kudos
1 Solution
PaulF_IntelCorp
Employee
1,372 Views

Android 4.4 is the first version where the HTML5 runtime engine is based on Chrome, so that represents a big change in the interpreter. My suspicion is you are running into some problem with CSP rules or similar, such as running JavaScript in an html file has been disabled, or something similar.

Try putting your JS in a js file and don't use "onclick" directly in the html. See the "blank cordova starter app" for an example of how to separate your html from you js. Specifically, see these lines > https://github.com/gomobile/sample-cordova-starter-app/blob/master/www/js/init-app.js#L70 <

View solution in original post

0 Kudos
2 Replies
PaulF_IntelCorp
Employee
1,373 Views

Android 4.4 is the first version where the HTML5 runtime engine is based on Chrome, so that represents a big change in the interpreter. My suspicion is you are running into some problem with CSP rules or similar, such as running JavaScript in an html file has been disabled, or something similar.

Try putting your JS in a js file and don't use "onclick" directly in the html. See the "blank cordova starter app" for an example of how to separate your html from you js. Specifically, see these lines > https://github.com/gomobile/sample-cordova-starter-app/blob/master/www/js/init-app.js#L70 <

0 Kudos
Peter_S_
Beginner
1,372 Views

It worked, thanks a lot. I just put my click event in "init-app.js", inside of the function "app.initEvents". Thus, I think it was not a problem with CSP rules. My JS code had just to be loaded when the app initalizes. It worked in the emulator from Android 4.0 to 7.0. Thanks.

0 Kudos
Reply