The problem is, I'm making a program like a chat, that needs to know if the user has left the window, or changed to another window or tab, to allow other users see that the other user is not seeing the page right now.
I thought the window event focusout
would solve my problems, however, there are a few issues with it.
First:
it does not fire only when the user leaves the window, if they focus on a input field then click anywhere else in the page, the event fires. Obviously that's intolerable.
I managed, in Firefox, a way around that. On Firefox, when that happens, the browser fires the focusout
event once. If you really leave the window however, it fires it twice. So, a little programming made the magic.
Then came the second problem:
Chrome, and I believe other browser might behave the same, only fires focusout
event once, no matter what you do. Leaving the window, changing focus from inputs to page, it's the same, so, my programming didn't worked there.
Does anyone know a way to simulate the desired behavior? Or a way to make Chrome and other possible browser to behave like Firefox or whatever?
The problem is, I'm making a program like a chat, that needs to know if the user has left the window, or changed to another window or tab, to allow other users see that the other user is not seeing the page right now.
I thought the window event focusout
would solve my problems, however, there are a few issues with it.
First:
it does not fire only when the user leaves the window, if they focus on a input field then click anywhere else in the page, the event fires. Obviously that's intolerable.
I managed, in Firefox, a way around that. On Firefox, when that happens, the browser fires the focusout
event once. If you really leave the window however, it fires it twice. So, a little programming made the magic.
Then came the second problem:
Chrome, and I believe other browser might behave the same, only fires focusout
event once, no matter what you do. Leaving the window, changing focus from inputs to page, it's the same, so, my programming didn't worked there.
Does anyone know a way to simulate the desired behavior? Or a way to make Chrome and other possible browser to behave like Firefox or whatever?
Share Improve this question edited Oct 8, 2020 at 8:55 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 28, 2011 at 14:48 Uriel BertocheUriel Bertoche 8832 gold badges14 silver badges23 bronze badges 5- I don't think that what you want to do is actually possible with today's browsers. – Pointy Commented Feb 28, 2011 at 14:52
-
Did you try
$(document).bind("mouseleave", function(){...})
? – Mottie Commented Feb 28, 2011 at 14:56 -
@Pointy The
window.onblur
handler works just fine. The blur event fires at the window object whenever the user switches to another tab. – Šime Vidas Commented Feb 28, 2011 at 15:09 - @Šime Vidas well I've had problems with it, but that was some time ago and I haven't tried lately. If it works, then that's good news for the OP at least :-) – Pointy Commented Feb 28, 2011 at 15:11
- Yeah, that works just as Šime sad it would. – Uriel Bertoche Commented Feb 28, 2011 at 15:15
3 Answers
Reset to default 10Use blur
instead of focusout
:
$(window).blur(function() {
// code
});
The difference is that focusout
bubbles up the DOM tree and blur
doesn't. If you set focusout
on the window object, then all the blur events that occur on page elements will also trigger that handler which is something that you don't want.
The difference is that focusout
(when set on the window object) captures all blur events that fire on the page (at any element). In contrast blur
(when set on the window object) does not capture those blur events.
Live demo: http://jsfiddle/simevidas/taRG6/
Does this answer it? Talks about using blur event
JavaScript / jQuery: Test if window has focus
I answer the question, this solution works perfectly except that i catch the event twice.
BLUR event on window is fired only when i click another brower tab or when i switch to another application.