Software Archive
Read-only legacy content
17060 Discusiones

JavaScript not working on Android 4.4+

Peter_S_
Principiante
3.044 Vistas

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 Solución
PaulF_IntelCorp
Empleados
3.044 Vistas

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 <

Ver la solución en mensaje original publicado

2 Respuestas
PaulF_IntelCorp
Empleados
3.045 Vistas

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 <

Peter_S_
Principiante
3.044 Vistas

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.

Responder