1

I have a web-application for iPhone, and I need to trigger a Javascript function when the web page is in focus, in other words, when Safari is open.

What I want to accomplish is to start a timer in some way when the user clicks on a tel-link and starts the call. When the call ends, Safari pops up again, and the timer ends.

Is there any way to do this?

Best Regards

Linus

dda
  • 6,030
  • 2
  • 25
  • 34
Rufuhs
  • 74
  • 7
  • Possible Duplicate of http://stackoverflow.com/questions/4656387/how-to-detect-in-ios-webapp-when-switching-back-to-safari-from-background – Simon Jul 22 '12 at 11:01
  • for a good advice we need more infos. is jquery used? is there a usage of sencha, jqm etc.? phonegap/cordova in use? – liquid Jul 22 '12 at 11:30
  • @liquid I am using jQuery to get the click on the link that starts the call. There is no other frameworks in use. I have tried to use $('html').focus() but it does not work. – Rufuhs Jul 22 '12 at 11:53
  • so you have a webpage, took it on the homescreen, user click to start a call and you want to track the time and then you want to open page in safari-browser to show it. is this correct? – liquid Jul 22 '12 at 12:07
  • @liquid exactly. After the call it already opens up the webpage, so all i need is something that will trigger a function that will show the time between the click on the link and the focus on the page! – Rufuhs Jul 22 '12 at 12:19

1 Answers1

1

try this:

if you trigger the link for the call set the actual time in a localStorage-item.

$("#yourButton").click(function() {

  var actualTime = new Date().getTime();
  window.localStorage.setItem('callStart', actualTime);

})

after that you need to read the Storage after user ends up the call. You can set this in the document.ready on the opening page.

in $(document).ready(function() {})

// check for the localStorageItem
if (window.localStorage.getItem('callStart')) {

  // get it
  var timeStart = window.localStorage.getItem('callStart');

  var now = new Date().getTime();

  /*
   Now calculate here the difference now - timeStart
   and you will get seconds, minutes or whatever you want
  */

  // !!! Dont forget to clear the localStorageItem
  window.localStorage.removeItem('callStart');

}

This is what I would try. The Usage of the HTML5-localStorage gives you the possibility to store key/values and data isnt lost if user stops the app or device is automatically locked.

Hope this helps a bit.

ADDED: You even can store JSON as the value in the localStorageItem. So you can set an callID and implement a calling-history for your users.

liquid
  • 175
  • 1
  • 8