Kody R. Kody R. -4 years ago 170
Javascript Question

deviceready not firing in cordova

The deviceready isn't firing correctly (at least in iOS) for my cordova project. I have searched for hours and still cannot figure this out. Am I doing something incorrectly? The path to js/cordova.js exists as well:

<html>
<head>
<title>Geolocation</title>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, target-densityDpi=device-dpi, user-scalable=no" />
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/app.css">
<script type="text/javascript">

// Fastclick
if ('addEventListener' in document) {
document.addEventListener('DOMContentLoaded', function() {
FastClick.attach(document.body);
}, false);
}

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

// Wait for device API libraries to load
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}

// device APIs are available
//
function onDeviceReady() {
alert("ready");
// Now safe to use device APIs
}

</script>
</head>
<body onload="onLoad()">


I'm not sure what I'm missing as I'm not getting any errors if I inspect in Chrome. Thanks for the help!

Answer Source

deviceready event is essential to any application. It signals that Cordova's device APIs have loaded and are ready to access.

Update

So I got "deviceready" to work by removing the onload="onLoad()" from the body and replacing this:

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

with just this:

document.addEventListener("deviceready", onDeviceReady, false);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download