Everyone knows that Mobile Safari pauses Javascript execution on a webpage when
- you move to a different browser tab
- switch to another iOS app (for example, when you receive an incoming call in the phone app)
To detect impending suspension and restoration of Javascript, you can listen for the window's "pagehide" and "pageshow" events.
The issue is that these events do not trigger when tab-switching (1.) in iPad's Mobile Safari. On iPhone's Mobile Safari, everything works as expected.
This problem can be easily demonstrated:
<!DOCTYPE html>
<html>
<head>
<script>
window.addEventListener("pagehide", function(evt){
var log = document.getElementById('log_id');
log.innerText = log.innerText + " pagehide fired!";
}, false);
</script>
</head>
<body>
<div id="log_id"></div>
</body>
</html>
On iPads (iOS5 and iOS6 Preview3), this event only triggers with app-switching (2.) and not with tab-switching (1.). All iPhones work correctly.
Has anyone found a way to detect an impending tab switch in the iPad browser?
The re-activation of Javascript when the tab becomes active again can be detected using a heartbeat loop as discussed in this related thread.