I have a simple PhoneGap application as fallows:
PhoneGap powered App
The main reason for unfired ondeviceready event on one or more platform, is when you try use cordova/phonegap js of the wrong platform.
Check the versions of the Cordova/phonegap library/jar files that you have added with the project (under libs) and the < script src="~/Scripts/cordova-3.0.0.js"/> script that you are referring in the HTML files. If there is a mismatch, the < script/> may not be able to refer it in the project. So that cordova fails to execute it's functionality.
Try it this way :
document.addEventListener("deviceready", function(){
alert("123");
},true);
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.
In my case, I needed to remove meta http-equiv="Content-Security-Policy" content="...
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.