I want to detect when the user leaves my page( e.g open a new tab) so I can stop a countdown. I did it using:
$(window).blur(function() {
//stop countdown
}
One solution would be to check if the iframe has the focus and then not stop the timer. e.g.
$(window).blur(function () {
// check focus
if ($('iframe').is(':focus')) {
// dont stop countdown
}
else {
// stop countdown
}
});
Now this would work, however if your iframe has the focus when the user changes tab, the countdown would not stop. So in this situation you would need to think of an elegant solution to move focus away from the iframe prior. For instance, if a user clicks within the iframe, immanently move focus back to the parent window.
Edit - Updated answer to include extra iframe functionality
Ok so I have been playing about with this. Now I don't know what content you have within your iframe, but you can add some code to this which basically sends the focus back to an object within the parent window when clicked on. e.g.
In your iFrame
In your main page
This will allow you to click on the iframe, set focus back to the main page, and then stop the countdown.