Re: [PhoneGap] How to load an internal resource

105 views
Skip to first unread message

Simon MacDonald

unread,
Aug 16, 2012, 9:41:42 AM8/16/12
to phon...@googlegroups.com
Hey Remy,

The reason you see "JSCallback Error: Request failed" is that the
CallbackServer has been shut down when you navigated from the current
page that has PhoneGap loaded to the new page you specified via
window.location. It's not the most descriptive log we've ever posted.

Simon Mac Donald
http://hi.im/simonmacdonald


On Thu, Aug 16, 2012 at 8:26 AM, Remy Sharp <re...@leftlogic.com> wrote:
> I might be being thick here, but I've created a local file to my device (via the FileWriter API - which works), and I'm doing `window.location = file.toURL();` which does actually work.
>
> But I'm seeing Cordova throwing an error "JSCallback Error: Request failed" - which I gather the connection between the client and Cordova have been shot dead (or at least switched to polling, which doesn't really make much sense to me).
>
> I'm testing on a real device on Jelly Bean / 4.1 (whatever's latest). Using PhoneGap 2.0.0.
>
> Should I be loading internal resources differently that using `window.location`?
>
> Cheers,
>
> - Remy
>
> --
> -- 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
>
> To compile in the cloud, check out build.phonegap.com
>
>

Remy Sharp

unread,
Aug 16, 2012, 9:56:50 AM8/16/12
to phon...@googlegroups.com
Is there any way to navigate to another document inside my phonegap app without the server shutting down?  I kinda need it up! :)

The new page that I'm navigating to includes the cordova-2.0.0.js script - but I assumed that doing a `window.location` would dump the current JavaScript state and reload a new state (and thus be able to reconnect to the server) - but you're saying the actual CallbackServer has gone away?

Cheers,

- Remy

Simon MacDonald

unread,
Aug 16, 2012, 10:05:18 AM8/16/12
to phon...@googlegroups.com
It gets restarted so don't worry. 
--
-- 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
 
To compile in the cloud, check out build.phonegap.com
 
 


--

Remy Sharp

unread,
Aug 16, 2012, 10:34:13 AM8/16/12
to phon...@googlegroups.com
Cheers.  I'm relatively confident that's the case now.

I had some weird race conditions that made me think the device ready event was firing at odd times, but I've a feeling I'm fine.

I should really post this separately, but looking at older threads, you might be the man that has the answer - I'm saving a new .html page (gets stored somewhere like file:///storage/sdcard0/page.html). That new HTML page needs to reference existing files in assets/www/ (like cordova and my own js scripts).  I tried referencing the scripts using /cordova-2.0.0.js, but it's finding them (which kinda makes sense).  

So, do you know how I can resolve the www path?

(huge thanks so far by the way!) :)

Simon MacDonald

unread,
Aug 16, 2012, 10:42:30 AM8/16/12
to phon...@googlegroups.com
The path to your www directory on Android is:

file:///android_asset/www

I've never tried to do what you are attempting but it should work. If
it doesn't let me know.

No problem with the help that's what I do. Sadly we will not get a
chance to meet IRL at PG Day EU but do me a favour and try to keep the
rest of the guys out of trouble. Oh, who am I kidding :)

Simon Mac Donald
http://hi.im/simonmacdonald


Remy Sharp

unread,
Aug 16, 2012, 10:50:26 AM8/16/12
to phon...@googlegroups.com
On Thursday, August 16, 2012 3:42:30 PM UTC+1, Simon wrote:
The path to your www directory on Android is:

file:///android_asset/www

But that doesn't translate across devices (or I had assumed it wouldn't), like on Blackberry, iOS, WP7, etc.  I'm trying to build something that goes right across *all* platforms :)

Or should I just hard code it and use something like `var root = "file:///" + device.platform.toLowerCase() + "_assets/www"` ?  Feels a bit nasty and brittle.

 
No problem with the help that's what I do. Sadly we will not get a
chance to meet IRL at PG Day EU but do me a favour and try to keep the
rest of the guys out of trouble. Oh, who am I kidding :)

It's my birthday the day of the workshop to boot, so I'm expecting to have a tipple or thirty - so yeah, I'm expecting trouble! :)


 
 

Simon MacDonald

unread,
Aug 16, 2012, 11:26:15 AM8/16/12
to phon...@googlegroups.com
I generally move the platform dependent stuff out to a separate .js or
.json file. That way it is out of the code. Here is a quick blog post
I wrote on the inconsistencies in the Media API and one way to handle
it:

http://simonmacdonald.blogspot.com/2012/01/on-eleventh-day-of-phonegapping.html

Looking back on that post I'd handle it differently today but you get the gist.

Simon Mac Donald
http://hi.im/simonmacdonald


Remy Sharp

unread,
Aug 16, 2012, 11:32:29 AM8/16/12
to phon...@googlegroups.com
I'm not sure I can actually do that, only because I need to include cordova-2.0.0.js and that'll be placed in the www by build.phonegap - so I need to be able to point my newly created page to the cordova script.

But yeah, I'm taking a similar approach:

  var root = {
    android: 'file:///android_asset/www'
  }[device.platform.toLowerCase()];

  var code = ['<script src="' + root + '/cordova-2.0.0.js"></script>',
              '<script src="' + root + '/spike.js"></script>',
              '<script>runSpike();</script>',
              '</head>'].join('\n');

Just need those other platform root paths.

Remy Sharp

unread,
Aug 16, 2012, 12:28:22 PM8/16/12
to phon...@googlegroups.com
This is why I was asking by the way: http://www.youtube.com/watch?v=vsmJq7tU_2o

:-)
Reply all
Reply to author
Forward
0 new messages