12

I am building an app, using polymer starter kit & cordova to wrap the project. Now, since I use firebase as a database for storing data, I ended up using two native firebase javascript function to find user data:

getAuth()

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
var authData = ref.getAuth();
if (authData) {
  console.log("Authenticated user with uid:", authData.uid);
}

onAuth()

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.onAuth(function(authData) {
  if (authData) {
    console.log("Authenticated with uid:", authData.uid);
  } else {
    console.log("Client unauthenticated.")
  }
});

Those two functions require a reload to bring back data from firebase, but :

window.location.reload();

doesn't work

Alos looked for a Cordova plugin: webview-reloader, installed it but redirect and reload still not working.

When i use the reload() function the screen of my android phone is going white and the application stop working. Need to close the app and open it again.

enter image description here

Dragod83
  • 2,127
  • 3
  • 17
  • 20
  • Did you try `document.location = 'index.html'`? http://stackoverflow.com/a/8506527 – Deurco Oct 01 '15 at 12:41
  • Did try document.location = 'index.html' and that throw an error, saying that he canno't find the file location – Dragod83 Oct 01 '15 at 13:16

3 Answers3

23

Since Cordova is a wrapper around a browser window:

window.location.reload(true);

It works for the browser windows as well as a Cordova app.

Vik
  • 3,495
  • 3
  • 22
  • 25
ShatrdWing
  • 509
  • 4
  • 11
9

This works for iOS and Android (at least with 2016 platform versions).

// 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;
}
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
0
if (this.cordovaService) { // is cordova
    this.window.location.href = `${this.window.location.protocol}//${this.window.location.host}`;
} else {
    this.window.location.reload();
}
patrick.elmquist
  • 2,113
  • 2
  • 21
  • 35
Alex Vitari
  • 51
  • 1
  • 3
  • This answer was reviewed in the [Low Quality Queue](https://stackoverflow.com/help/review-low-quality). Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / **better** than existing / accepted answers. – Trenton McKinney Sep 05 '22 at 17:55