Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deviceready event not firing in Cordova 3.2.0

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.

like image 880
eversor Avatar asked Dec 16 '22 03:12

eversor


2 Answers

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).

like image 133
eversor Avatar answered Dec 28 '22 10:12

eversor


The problem could be that the devicereadyevent 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.

like image 38
Ignitor Avatar answered Dec 28 '22 11:12

Ignitor