Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create event listener on Window.Blur () event?

Javascript: How to create event listener on Window.Blur() event?

like image 900
Rella Avatar asked Dec 03 '10 20:12

Rella


People also ask

What is blur in addEventListener?

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.

What is blur () method?

blur() method removes keyboard focus from the current element.


2 Answers

window.onblur = function() {
   //say goodbye
};

According to quirksmode onfocus and onblur are available on the window in most browsers.

like image 74
user113716 Avatar answered Nov 02 '22 20:11

user113716


It's better to use addEventListener instead of a property, this way you can set multiple handlers and nobody (including yourself) will accidentally disable your handler by overwriting the onblur property.

window.addEventListener('blur', function() {
   console.log('blur');
});
like image 20
user Avatar answered Nov 02 '22 21:11

user