I am using Cordova 3.2.0-0.3.0 and NetBeans 7.4 to develop a Cordova application. Although it works properly in the Mobile's Chrome browser, it does not seem to work properly neither in my phone (Android 4.1.2) nor in the emulator (Android 4.3, API level 18).
The problem seems to be that the deviceready
event is never fired.
This is the code:
app.onReady = function(callback) {
$(document).ready(function() {
// are we running in native app or in browser?
window.isphone = false;
if (document.URL.indexOf("http://") === -1
&& document.URL.indexOf("https://") === -1) {
window.isphone = true;
}
if (window.isphone) {
alert("isPhone");
document.addEventListener("deviceready", callback, false);
} else {
callback();
}
});
};
app.onReady(function(){ alert("test"); });
"test" it is never shown.
As ignitor guessed, I forgot to include the cordova.js
.
Though I must say, in my defense (:P), that it is weird to include a script like if it was on your root when it is not there (at least when testing in a browser).
The problem could be that the deviceready
event is triggered before the DOM is ready, i.e. before the $(document).ready()
callback is executed.
You should try to bind to deviceready
independently of $(document).ready()
.
See this answer for an example of how to do that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With