jQuery Plugin To Detect If The Browser Gets Or Losts Focus

File Size: 4.99 KB
Views Total: 622
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
jQuery Plugin To Detect If The Browser Gets Or Losts Focus

winFocus is a lightweight jQuery plugin which allows to fire events when the browser window / tab gets or loses focus.

How to use it:

1. Just include jQuery library and the jQuery winFoucs plugin on the webpage and we're ready to go.

<script src="//code.jquery.com/jquery.min.js"></script>
<script src="jquery.winFocus.js"></script>

2. Trigger an event when current browser window / tab gets focus.

$.winFocus(function(event, isVisible) {
  // do something
  console.log("Focus", isVisible);
});

// or
$.winFocus({
  focus: function(event) {
    // do something
    console.log("Focus");
  },
});

3. Trigger events when current browser window / tab gets or loses focus.

$.winFocus(function(event) {
  // do something
  console.log("Blur", event);
},
function(event) {
  // do something
  console.log("Focus", event);
});

// or
$.winFocus({
  blur: function(event) {
    // do something
    console.log("Blur");
  },
  focus: function(event) {
    // do something
    console.log("Focus");
  },
});

4. Trigger events whenever current current browser window / tab's focus changes.

$.winFocus({
  blurFocus: function(event, isVisible) {
    // do something
    console.log("Combo", isVisible);
  }
});

This awesome jQuery plugin is developed by JDMcKinstry. For more Advanced Usages, please check the demo page or visit the official website.