Re: "ReferenceError: Can't find variable: cordova" - index.html:1

4,654 views
Skip to first unread message

Matt Gowie

unread,
Dec 8, 2012, 1:54:18 PM12/8/12
to phon...@googlegroups.com
Hm.. I feel like you're right in that it can't connect to the api on the device. I am only loading in JSON files from the web, and then when they're actually loaded I pull down a bunch of images as well, but my application never even gets that far down the line. None of the console logs which I have at those points of execution get run, so it must be timing out before that. 

I think I am gonna try reinstalling cordova 2.2. 

On Friday, December 7, 2012 1:17:56 PM UTC-5, Nathaniel Foster wrote:
I think what the error is saying is that it cannot connect with the cordova api on the native device.  I am receiving the same error from the Safari Web Inspector.  I am loading an external .aspx page into my iOS phonegap 2.2 app.  One thing that seemed to cause the error was my .aspx page was checking if the device was iOS and dynamically adding additional javascript files to the page.  These additional javascript files were added after the initial javascript files had downloaded.  After I removed the additional js files, I still receive the error, albeit less frequently.  Now it seems that whenever some images in the site download after the cordova-2.2.js file downloads I receive the error.  If the images download before the javascript then my app works fine.  I do not know if I am treating the symptoms of some larger problem or treating the actual problem.  I would look at the order in which your files are downloading, there might be a clue there.

On Thursday, 6 December 2012 22:20:33 UTC-5, Matt Gowie wrote:
I am getting the above error from the Safari Web Inspector when I do 'window.location.reload()' in the console and reload the project on my iPhone. It is the first thing that gets outputted. My project's app.js file gets run but, but onDeviceReady is never called. I started to try and debug this by adding console logs here and there, but the error continues to come up first all the time. I even removed the require.js script line from my index.html file (which means no Javascript files were running!) to see if the error persisted, and it did. So I have something going wrong with my xCode/Cordova project which is causing this problem, but I have no idea what it is as I am seeing nothing when compiling/building the project. The only recent change I made was that I added the ShareKit plugin a couple weeks ago, which actually gave me a lot of problems, but I had all of that figured out after a week. The app has been running fine, but suddenly it's giving me this error and I really don't know where it's coming from. 

Has anyone run into this problem or know of anyway I can further debug the problem so that I can find out where the error is coming from? The js bug which points to the HTML5 header for the index.html file is of no help at all, so I really don't know where to go from there. 

Thanks!

Gowie

unread,
Dec 9, 2012, 1:12:04 PM12/9/12
to phon...@googlegroups.com
I believe I've figured out this problem. For some reason my requireJS main.js file wasn't loading the cordova.js file first like it had been previously, and from what I have figured there must have been some sort of Objective-C code that calls to the index.html file to check/run/do something with the cordova variable sometime during load. I was never able to find this code, but if I removed all traces of JavaScript the 'Can't find variable: cordova' occured, so something which is dynamically added or called on the web files had to be causing that error.  I realized if I added script to get cordova at the bottom of my body tag it loaded it and no error was thrown.
Message has been deleted

Nathaniel Foster

unread,
Dec 12, 2012, 9:41:58 AM12/12/12
to phon...@googlegroups.com
So just to clarify, cordova.js is loading last and it is working better?  Also do you load everything else first then dynamically add cordova.js to the bottom of the page? I am still having some occasional problems receiving the error.  Thanks

Gowie

unread,
Dec 12, 2012, 12:04:34 PM12/12/12
to phon...@googlegroups.com
Yeah, thats the gist of it. 

Here's exactly what I did to fix the problem (Keep in mind I'm using require.js to load scripts):

1. Added cordova.js script to bottom of my index.html

2. In main require.js (main.js) file which loads all my scripts and starts my app.js file I removed the loading of cordova.js

3. Removed the "require(['cordova'], function()..." from my app.js file.

4. Switched in my "$(document).ready(function()..." the line where I call "$(document).bind('deviceready', onDeviceReady);" to "document.addEventListener('deviceready', onDeviceReady, false);" as it seems codova-2.2.0.js no longer supports a jQuery bind. This is faster anyway.

I think that's about it. Hopefully it works out for you!

Matt Gowie

unread,
Dec 13, 2012, 3:20:06 PM12/13/12
to phon...@googlegroups.com
Yes. It is now only loaded at the bottom of my document. 

On Thursday, December 13, 2012, colin mcdonald wrote:
so are you saying you completely removed cordova from the requirejs part of your code?  

colin mcdonald

unread,
Dec 13, 2012, 3:23:34 PM12/13/12
to phon...@googlegroups.com
Thanks.  Actually, I came across this and it was very helpful for me to get phonegap/requirejs/parse all up and running.

Torsten Barthel

unread,
Jun 16, 2014, 3:01:53 PM6/16/14
to phon...@googlegroups.com
Hi there, 

I came across this cause I had the same problem while developing a phonegap application with wildabeast barcode scanner plugin. I realized quickly that in my index.html there was no reference to cordova.js. As I added this line at the bottom of the document right before the closing body tag and compiled through 'cordova build ios' at the CLI. At this point it worked like a charm and my application with integrated barcode scanner was up and running :D

So guys have a good time and much respect!
Reply all
Reply to author
Forward
0 new messages