Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 459
Kody R. Avatar asked Mar 11 '16 04:03

Kody R.


2 Answers

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

like image 104
Lydia halls Avatar answered Oct 12 '22 18:10

Lydia halls


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.

like image 35
Elliot B. Avatar answered Oct 12 '22 20:10

Elliot B.