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.

Roope Hakulinen
  • 7,326
  • 4
  • 43
  • 66
eb1
  • 2,897
  • 3
  • 31
  • 42

4 Answers4

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

Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
11

That did not initially fix it for me until I removed

< meta http-equiv="Content-Security-Policy" content=".." / >
jkdev
  • 11,360
  • 15
  • 54
  • 77
A. Soufi
  • 133
  • 1
  • 4
  • 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 referenced – ferdil Sep 26 '16 at 07:15
  • Thanks for the tip. Why it happens? I don't have any remote scripts – Fábio Nicolau de Lima Jun 20 '17 at 18:21
  • 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! – Britton Pentakill Oct 08 '19 at 06:27
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.

Elliot B.
  • 17,060
  • 10
  • 80
  • 101
  • 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*. – Jacksonkr Nov 07 '19 at 18:13
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

Abraham Brookes
  • 1,720
  • 1
  • 17
  • 32