Skip to content Skip to sidebar Skip to footer

DeviceReady Not Working In PhoneGap Application, How To?

I have a simple PhoneGap application as fallows: PhoneGap powered App Copy

Solution 2:

What @GPRathour provided works because document.addEventListener() is listening for deviceready, then if true, running your alert function. I didn't work how you had it because of two reasons:

1) when the DOM loaded and got down to your body tag it was calling OnDeviceReady() and the listener never got the call, so phonegap doesn't know it's ready to run.

2) you would have to call your listener from within a function and use 'false':

function init(){
  document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady(){
  alert('123');
}

<body onload="onDeviceReady()"></body>

Check out the phonegap API as to why to use false instead of true in your listener, has to do with the default setting, but it's worth the read to understand how phonegap listeners work.


Solution 3:

Just in case you have the same problem as me, I didn't know is needed to include the cordova.js script in your index.html, you don't have to provide the file or the reference, just include this line:

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>  

and then Cordova will use the library when compiling, after that the event is dispatched.


Solution 4:

this code work for me

<body onload="init()"></body>

function init() {
    document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
    // Now safe to use the Cordova API
}

happy coding.......


Solution 5:

When using PhoneGap 3.0 with WP8 Device Ready will not work because Phonegap.js is NOT INCLUDED in the Visual Studio solution.

The solution is to include it manually for now.


Post a Comment for "DeviceReady Not Working In PhoneGap Application, How To?"