1

I have a very basic HTML5 page setup for offline use which tells me when it is offline and when it is online.

I've added location tracking to it to record the position of the device while it is running:

function getLocation() {            
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(success, error);
    } else {
      alert('geolocation not supported');
    }
    setTimeout(function() { getLocation(); },5000);  // calls itself every 5 seconds
}  

This seems to work well, however there is a small problem.

On a desktop browser, you can minimize the page and the location keeps being recorded.

On a mobile device (i.e. iOS), when you minimize the browser, it is suspended i.e. the location no longer gets recorded.

Is there a way to force a webpage to be 'kept alive' on mobile devices? I know there's a similar feature for apps where you can go back to the homescreen and the app keeps running.

As you can see, this is not a "session being lost" issue, it is simply that all scripts on the page are stopped while the web page is not being viewed.

Techmonk
  • 1,459
  • 12
  • 20
cusimar9
  • 5,185
  • 4
  • 24
  • 30
  • 1
    Have you tried [Web Workers](http://www.w3schools.com/html/html5_webworkers.asp)? Though I doubt it'll work on iOS, since the system is quite strict about code running in the background... – jjv360 Feb 26 '13 at 11:23
  • I think that would depend more on the browsers control then yours. If the browser is designed to sleep on quit or something then you wont be able to get around it. As an app you should be able to get that control from the OS – Techmonk Feb 26 '13 at 11:23
  • @jjv360 caniuse.com/#search=webworkers working on ios, but not on android at all – Toping Feb 26 '13 at 11:24
  • Thanks for the tip jjv360 but it doesn't look like you can get a user's position inside a web worker: http://stackoverflow.com/questions/11533838/html5-navigator-geolocation-in-web-workers – cusimar9 Feb 26 '13 at 11:58

2 Answers2

2

You can't force a page to stay active - this is out of your control as the operating system suspends or tomb-stones the browser to save resources.

Fenton
  • 241,084
  • 71
  • 387
  • 401
0

You can try to for loop an audio an file and play it and this will help the browser to run in the background forever.