Cordova DeviceReady not firing
Asked Answered
U

4

25

I've been banging my head on this one for a few days. After getting a number of "object not defined" errors when trying to create a new FileTransfer() object, it looks like the problem is more basic -- somehow the DeviceReady event is not firing.

Stack Overflow has a lot of hits on this issue, but most of them have to do with pre-3.x cordova builds that had a different architecture (I'm on 4.1.2). I've tried the suggestions in the newer topics I could find -- removing and adding plugins, updating cordova, etc. -- to no avail. To try to isolate the issue, I've commented out the startup code to just a few lines:

Index.html:

<!DOCTYPE html>
<html>
<head>
  <title>Blah</title>
  <meta charset="utf-8"/>
  <meta name="viewport" content="initial-scale=1, user-scalable=no, minimum-scale=1, maximum-scale=1">
  <link href="res/topcoat/css/topcoat-mobile-light.min.css" rel="stylesheet">
  <link href="res/css/styles.css" rel="stylesheet">
  <link href="res/css/pageslider.css" rel="stylesheet">
  <script data-main="js/main" src="lib/require.js"></script>
</head>

Main.js:

require(["app/Application"], function (Application) {
  "use strict";

  document.addEventListener("deviceready", function(){
      $('body').html("<p>device is ready</p>");
  },true);

  $('body').html("<p>waiting...</p>");
});

Instead of displaying "device is ready" in the body after a bit, the screen just displays "waiting...". This happens on both the iOS emulator and the browser (cordova emulate browser).

Cordova info:

$ cordova -v
4.1.2

Plugin info:

$ cordova plugins
org.apache.cordova.globalization 0.3.3 "Globalization"

(I get the same results if Globalization isn't there).

Is there some other place I should be looking? I'm running from the command line, if that makes a difference.

Uncommercial answered 9/1, 2015 at 16:47 Comment(0)
I
52

I think in this case you need to include cordova.js in your application, because I don't see cordova.js in your example

<script src="cordova.js"></script>

Note: path to cordova.js depends on where it located in your app

Ignoble answered 9/1, 2015 at 16:53 Comment(0)
U
11

That did not initially fix it for me until I removed

< meta http-equiv="Content-Security-Policy" content=".." / >
Utah answered 11/8, 2016 at 5:8 Comment(3)
Thanks - This led me to my problem, and it could be related to the initial problem as well. I've found this on iOS, but not on Android, when one of your Javascript files references a remote script or stylesheet, then it must be listed in the Content-Security-Policy. With Android you get an error message in the console. With iOS, it just dies silently and doesn't indicate what the problem is. The problem is that it cannot get to one of the scripts being referencedTeodoro
Thanks for the tip. Why it happens? I don't have any remote scriptsBosporus
This was my issue as well... an automatically added by cordova meta content security tag that prevented it from firing the onready. Thanks for the tip!Brooch
B
2

I recently 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.

Barrier answered 7/6, 2017 at 18:45 Comment(1)
Please note! I had some bad permissions (desperate times) inside my plugins folder so I need to use sudo when removing. do not use sudo when adding a anything.Kish
F
0

I have been trying to fix this issue for DAYS and I finally got deviceready to trigger. The issue was that I had extended the js Object to insert my own hide and show commands. Removing those lines allowed deviceready to trigger:

Object.prototype.hide = function(){
    this.style.display = 'none';
}

Object.prototype.show = function(){
    this.style.display = 'initial';
}

note: I also had to have the line <script src="cordova.js"></script> as mentioned by Alexander T

Fala answered 21/1, 2018 at 22:29 Comment(1)
I have since learned that it's really bad practice to extend the javascript Object. Don't do it FYIFala

© 2022 - 2024 — McMap. All rights reserved.