Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-browser: detect blur event on window

I just read, I think all the thread that deals with this subject, and I can't find a real solution to my problem. I need to detect when the browser window loses its focus, i.e. a blur event. I've tried all the scripts on stackoverflow, but there doesn't seem to be a proper cross-browser approach.

Firefox is the problematic browser here.

A common approach using jQuery is:

window.onblur = function() { 
   console.log('blur'); 
}
//Or the jQuery equivalent:
jQuery(window).blur(function(){
    console.log('blur');
});

This works in Chrome, IE and Opera, but Firefox doesn't detect the event.

Is there a proper cross-browser way to detect a window blur event? Or, asked differently, is there a way to detect a window blur event with the Firefox browser?


Related questions and research:

  • See Firefox 3 window focus and blur
  • According to the following github articles, jQuery has discontinued support for Firefox blur testing:
    • https://github.com/jquery/jquery/pull/1423
    • http://bugs.jquery.com/ticket/13363
like image 815
M4nch4k Avatar asked Feb 01 '13 13:02

M4nch4k


People also ask

What triggers blur event?

The blur event fires when an element has lost focus. The event does not bubble, but the related focusout event that follows does bubble. The opposite of blur is the focus event, which fires when the element has received focus. The blur event is not cancelable.

How do you stop event blurs?

Preventing the blur If you want to prevent the blur event from being fired, you have to do so when you are inside the mousedown event, you can do so by invoking the method preventDefault() on the event. Click the checkbox, focus input & then click the button, the textfield never loses focus now.

What is window blur?

Window blur() The blur() method removes focus from a window. The focus() method sets focus to a window.

When would an on blur event occur?

The onblur event occurs when an object loses focus. The onblur event is most often used with form validation code (e.g. when the user leaves a form field). Tip: The onblur event is the opposite of the onfocus event.


2 Answers

I tried both:

document.addEventListener('blur', function(){console.log('blur')});

and

window.addEventListener('blur', function(){console.log('blur')});

and they both worked in my version of FF (33.1).

Here's the jsfiddle: http://jsfiddle.net/hzdd06eh/

Click inside the "run" window and then click outside it to trigger the effect.

like image 105
Todd Avatar answered Oct 06 '22 04:10

Todd


It appears that jQuery no longer supports these tests for FireFox:

  • jQuery bug ticket is here: http://bugs.jquery.com/ticket/13363
  • jQuery close/deprecation commit is here: https://github.com/jquery/jquery/pull/1423

I am searching for a better way to support Firefox blur eventing, but until I find a better approach, this is a more current status relative to the original accepted answer.

like image 44
tohster Avatar answered Oct 06 '22 05:10

tohster