Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Peter_S_
Beginner
252 Views

JavaScript not working on Android 4.4+

Jump to solution

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>

Tags (2)
0 Kudos

Accepted Solutions
PaulF_IntelCorp
Employee
252 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
253 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
Peter_S_
Beginner
252 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