13

Is there any way to do this?

E.g., if a user starts the app with no internet connection, no remote scripts can be loaded, and the application basically can't run and I display a "No internet" page. But if the user gets internet later and the application is still running, is there any way to just "restart" ?

3 Answers 3

17

how about -

document.location = "index.html"

PhoneGap applications are just like an embedded website - you should be able to go to any hyperlink you wish (mind the whitelists).

Of course, if you also want to detect when it's again online, you should use the PhoneGap Network API to bind to those online/offline events.

In general thought, have you ever thought of using the HTML5 manifest functionality to actually let your local PhoneGap app cache those remote scripts? That way your app could still run, even when offline (except if it needs remote data to "do your thing")...

Hope this helps!

3
  • hugely helpful, thanks. i have not considered caching the scripts - would that work with the google maps api do you think?
    – Justin
    Dec 19, 2011 at 18:46
  • the main issue this clears up for me are javascript timeout errors. i know there has been much debate, but it is clear that uiwebview does not have nitro and simply can't execute javascript like mobile safari does. my jqm app worked flawlessly in mobile safari but kept crashing in uiwebview - but since phonegap never really "restarts" and the javascript errors don't cause the actual app to crash, it would simply become unresponsive. document.location does the trick to fix things.
    – Justin
    Dec 19, 2011 at 18:48
  • this might fail if you use html5 url routing for your app (as the index.html might not be relative anymore). For that consider using this: stackoverflow.com/a/37837475/82609 Jun 15, 2016 at 13:52
1

Try this

navigator.app.loadUrl("file:///android_asset/www/index.html", {wait:2000,  loadingDialog:"Wait,Loading App", loadUrlTimeoutValue: 60000});
0
0

Accepted solution works, but might fail if you have an SPA with html5 url routing.

Here's a safest solution:

// keep startup url (in case your app is an SPA with html5 url routing)
var initialHref = window.location.href;

function restartApplication() {
  // Show splash screen (useful if your app takes time to load) 
  navigator.splashscreen.show();
  // Reload original app url (ie your index.html file)
  window.location = initialHref;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.