[PhoneGap] Android - Issue with deviceready and window.location

7,332 views
Skip to first unread message

Jacob

unread,
Sep 30, 2011, 1:21:48 AM9/30/11
to phonegap
I've discovered an issue with working between two HTML files in
Phonegap on Android and the deviceready event.

1) I have an index.html page that triggers deviceready fine
2) I have another page that is simply a demo page from the Phonegap
wiki docs.

When I redirect to a page within Javascript based on a server response
from Facebook, I use window.location.href="pagename.html". The page
loads fine, but deviceready is not triggered. However, if I move this
same page to be the first page loaded by Phonegap, it triggers
deviceready fine.

I ran a test to see if it was an isolated issue with
window.location.href by placing a simple link on the homepage, and
when I loaded the second page through that link, deviceready triggered
fine. It seems that if I want to navigate to sub-pages, I can only do
so through href links and not via javascript? I saw Simon's example of
<a href="#" onclick="window.location='home.html'>link</a>, but that is
a different scenario since the user isn't clicking a link for this to
happen. It happens from a JSON request result.

Appreciate any tips/ideas before I end up re-writing and hacking this
together incorrectly just to get deviceready to work.

Cheers,
Jacob

bass

unread,
Sep 30, 2011, 3:49:57 AM9/30/11
to phonegap
If the deviceready event already has triggered, you can test with
if(device){}.

Jacob

unread,
Sep 30, 2011, 2:16:34 PM9/30/11
to phonegap
It's definitely not triggering on second page ever if the page is
loaded via window.location within JS. 'device' is never constructed.

Uncaught ReferenceError: device is not defined at file:///android_asset/www/main.html:11

Bryce Curtis

unread,
Sep 30, 2011, 3:28:11 PM9/30/11
to phon...@googlegroups.com
PhoneGap gets notified whenever a new page is to be loaded.  However, there appears to be a bug in Android that prevents this from happening when window.location is set.  As a result, the new page is loaded in place of the existing page and PhoneGap doesn't have any way to intercept the request.  

As a solution, instead of using window.location, use

navigator.app.loadUrl("file:///android_asset/www/home.html");

Note that you have to include the entire URL, not just relative part.

Bryce Curtis

unread,
Sep 30, 2011, 3:59:18 PM9/30/11
to phon...@googlegroups.com
The Android bug for this is http://code.google.com/p/android/issues/detail?id=9122

Star it if you want to cast your vote for getting it fixed.

Jacob

unread,
Sep 30, 2011, 4:09:33 PM9/30/11
to phonegap
You're a genius... this fixed it. Thanks Bryce!

Yulya

unread,
Oct 3, 2011, 3:37:52 AM10/3/11
to phonegap
It doesn't work for me,
please look at the test project, cannot find an error by myself
(android build target 2.2, phonegap 1.0.0, jquery 1.6.2)

project www folder: http://dl.dropbox.com/u/20618915/Phonegap%20Reload%20Test/www.rar

Nimish Nayak

unread,
Oct 3, 2011, 4:09:57 AM10/3/11
to phon...@googlegroups.com
i think u need to include the phonegap.js file everywhere you use the phonegap API's


--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en

For more info on PhoneGap or to download the code go to www.phonegap.com

Yulya

unread,
Oct 4, 2011, 2:43:27 AM10/4/11
to phonegap
Solved
Manifest file caused the problem, if somebody faces the same, my
problem was with upgrading an app to phonegap 1.0.0, see
http://simonmacdonald.blogspot.com/2011/07/upgrading-your-phonegap-android.html
If manifest file contains only one activity, nothing happens after
logcat's:
...
App.loadUrl('file:///android_asset/www/home.html',null)
Starting activity: Intent, ...
...

Thanks everyone.

Pacer31

unread,
Oct 19, 2011, 1:23:56 PM10/19/11
to phon...@googlegroups.com
I've recently ran into this same problem.  Like you said the..using..navigator.app.loadUrl("file:///android_asset/www/login.html");

Appears to solve the deviceready issue.  But I'm running into a problem when using an emulator in Eclipse; sometimes I get the following here when it attempts to perform this function and at other times it works just fine?

An alert box pops up with the following information:
"The connection to the server was unsuccessful. (file:///android_asset/www/login.html")

Using Phonegap1.1.0, compiled with Android 3.2; 2.3 emulator.

Is it just an emulator issue or is my url not written correctly?

Jurgen B.

unread,
Nov 15, 2011, 9:39:29 AM11/15/11
to phonegap
@ Bryce, thank you for this piece of code. I have wasted a lot of time
on this... too bad it took me so long to find this answer.

@ Pacer, you were missing a double quote in front of file? But I hope
by now, you have figured that out already :)

Paul Beusterien

unread,
Dec 26, 2011, 11:53:05 AM12/26/11
to phon...@googlegroups.com
When I updated from PhoneGap 1.1.0 to 1.3.0, navigator.app.loadUrl stopped working to allow the loaded page to execute PhoneGap capabilities.

Any suggestions on how to get PhoneGap capabilities back?

Thanks,
Paul

Paul Beusterien

unread,
Dec 26, 2011, 2:27:03 PM12/26/11
to phon...@googlegroups.com
Found the solution. Android now needs a whitelist file.  This res/xml/phonegap.xml file keeps navigator.app.loadUrl working:

<?xml version="1.0" encoding="utf-8"?>

<phonegap>

    <access origin="http://127.0.0.1*"/>

    <access origin="http://192.168.0.104*"/>

    <log level="DEBUG"/>

</phonegap>


where 192.168.0.104 is the base of the url I'm passing to loadUrl. 


Robert Schmuecker

unread,
Apr 13, 2012, 12:35:25 PM4/13/12
to phon...@googlegroups.com
Hi Paul et al,

First of all, cracking work with Weinre and a big thanks to all who contribute - it's a formidable tool very valued by the community it serves.
I am adding this here as I am sure it has something to do with this topic.  I am trying to setup a dev enviroment following the tutorial at sencha http://www.sencha.com/learn/a-sencha-touch-mvc-application-with-phonegap/ which works absolutely fine on my Android emulator running Gingerbread 2.3.3 aswell as on a device with same operating system.  I am doing this on windows via Eclipse and have taken a headstart of that from the phonegap intro to Android However when I add in the script tag to connect to weinre it breaks the javascript for some reaons citing that the "device is not defined" after this being in the index.html with all relevant sencha files declared before this point.

<script type="text/javascript">
            document.addEventListener("deviceready", app.mainLaunch, false);
</script>       
<script src="http://10.0.0.104/target/target-script-min.js#anonymous"></script>

where 10.0.0.104 is the address of my dev machine.

This in turn calls the app.mainLaunch method which kicks up the error.

The method being called is as follows

mainLaunch: function() {
        if (!device || !this.launched) {return;}
        this.views.viewport = new this.views.Viewport();
    }

and the line where the error occurs is

if (!device || !this.launched) {return;}

As mentioned this only occurs when including weinre, without it the application deploys and runs as it ought to.  I notice that weinre does infact receive the connection when viewing the client of weinre which I have running on port 80.

I have also followed your above tip of adding the res/xml/phonegap.xml file with appropriate origin, in this case :-

<?xml version="1.0" encoding="utf-8"?>
<phonegap>
<access origin="http://127.0.0.1*"/>
<access origin="http://10.0.0.104*"/>

<log level="DEBUG"/>
</phonegap>


However I am still unable to launch the application due to the error (which I can see in the debug console of Eclipse the output of which I include below):-

04-13 18:16:28.259: E/Web Console(21504): Uncaught ReferenceError: device is not defined at file:///android_asset/www/app/app.js:8

I would very much appreciate some insight as to why this might be occuring and feel that this was the most appropriate place to ask.

Thanks kindly in advance for your efforts.

Rob.

Patrick Mueller

unread,
Apr 18, 2012, 8:34:32 AM4/18/12
to phon...@googlegroups.com
On Friday, April 13, 2012 12:35:25 PM UTC-4, rob1308 wrote:
I am adding this here as I am sure it has something to do with this topic.  

I've created a thread on the weinre google group for this:


Let's move the discussion there.
Reply all
Reply to author
Forward
0 new messages