11

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

Kody R.
  • 2,430
  • 5
  • 22
  • 42
  • `Chrome` ? You need to execute `APK` I guess.. – Rayon Mar 11 '16 at 04:11
  • @RayonDabre It doesn't fire on my iOS device either. It takes time to build and deploy so instead I usually check serve the front-end files up from python SimpleHTTPServer to check frequently – Kody R. Mar 11 '16 at 04:13
  • `deviceready` will fire in native app only..Not in device browser.. – Rayon Mar 11 '16 at 04:14
  • @RayonDabre I figured that might be the case, but it wasn't working either way, so I wasn't 100% certain – Kody R. Mar 11 '16 at 04:15
  • Execute it as `app` and if it does not work..Update your question accordingly.. – Rayon Mar 11 '16 at 04:17

4 Answers4

11

There's no need to have the cordova.js inside your www/js/ folder because that file is copied from another location to platforms/ios/platform_www (i.e. by running: cordova build ios) at the same level that the index.html file, so in order to have a proper configuration, the next statement:

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

must be changed to:

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
slackmart
  • 4,754
  • 3
  • 25
  • 39
  • Yes, but not due to that - I added my answer below. I have no idea why it didn't work the way before, but it is now! – Kody R. Mar 11 '16 at 04:36
9

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);
Rayon
  • 36,219
  • 4
  • 49
  • 76
Kody R.
  • 2,430
  • 5
  • 22
  • 42
  • @RayonDabre Oh that makes much more sense now! – Kody R. Mar 11 '16 at 04:38
  • 1
    @RayonDabre The origin post was copied and pasted from the cordova website so I felt it was safe to assume something that simple from their own website would work but apparently not – Kody R. Mar 11 '16 at 04:39
  • When the application starts up, we need to wait for the `deviceready` event so we can interact with parts of the PhoneGap API... – Rayon Mar 11 '16 at 04:40
  • Sorry for my interfere, @RayonDabre, Can you post where you copied (in the docs)? – slackmart Mar 11 '16 at 04:42
  • 1
    @slackmart https://cordova.apache.org/docs/en/latest/cordova/events/events.deviceready.html – Kody R. Mar 11 '16 at 04:45
  • This suggestion seems to guarantee that the plugins are loaded before executing DOM javascript code. I don't use the onLoad event anymore. – pollaris Oct 06 '17 at 14:46
  • I have added deviceready in the script tag of the html. It doesn't get fired when html is loaded. Instead, when clicked home button, when the app enters background, it gets fired. I am not sure what am I missing here. Any ideas? – nOOb iOS Nov 03 '17 at 12:21
6

I had the same problem, my solution was to add :

<script type="text/javascript" src="cordova.js"></script>

in the HTML file and everything worked perfectly

Torsten Barthel
  • 3,059
  • 1
  • 26
  • 22
Lydia halls
  • 652
  • 8
  • 17
4

I had this same issue, but in my case cordova.js was already properly included.

Eventually what worked for me was a simple remove and add of the ios platform:

cordova platform remove ios
cordova platform add ios

It had been quite a while since I had completely re-built the ios platform and other major changes had taken place during that time (Cordova upgrade, XCode upgrade, etc). It's possible that my config.xml or existing ios build was somehow incompliant with the latest Cordova requirements.

Elliot B.
  • 17,060
  • 10
  • 80
  • 101