To detect when safari is going background, you can use window's events called pageshow
and pagehide
(but this you already found out).
document.addEventListener('pageshow',function(){
// Do something here
}, false);
document.addEventListener('pagehide',function(){
// Do something here
}, false);
You can also use the PageVisibility API (available since iOS7) to check if the tab has changed.
document.addEventListener('visibilitychange', function(){
if (document.hidden) {
// Tab out of focus
}
else {
// Tab on focus
}
},false);
Note that this code should work on safari since iOS7, but some browsers needs prefixes.