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
});
But I have an Iframe inside my page and the countdown also stops when the user clicks on it, but I don't want the above event to execute when someone clicks on the Iframe.
Any idea?
Update, I'm trying a bit more, based on this answer Click-event on iframe?:
iframeDoc = $('iframe').contents().get(0);
$(iframeDoc).click(function(){
//maybe remove blur event?
});
Update: Tim B solution worked:
$(window).blur(function () {
// check focus
if ($('iframe').is(':focus')) {
// dont stop countdown
}
else {
// stop countdown
}
});
Now I have to remove focus from the Iframe every time the blur event is called, otherwise the countdown will not stop if the user changes tab after focusing the Iframe. I tried like this using the above condition:
if ($('iframe').is(':focus')) {
// dont stop countdown
$("iframe").blur()
$(window).focus();
}
But it did not work. Any idea?