3

I need to know about the visibility state of the tab when user does the tab switch in a particular browser and when user does the application switch.(switching out from browser)

var visibilityState,
    activeTab = (function(){
        var stateKey, eventKey, keys = {
            hidden: "visibilitychange",
            webkitHidden: "webkitvisibilitychange",
            mozHidden: "mozvisibilitychange",
            msHidden: "msvisibilitychange"
        };
        for (stateKey in keys) {
            if (stateKey in document) {
                eventKey = keys[stateKey];
                break;
            }
        }
        return function(c) {
            if (c) document.addEventListener(eventKey, c);
            return !document[stateKey];
        }
    })();
activeTab(function() {
    visibilityState = activeTab();
});

uses

$(window).blur(function(){
    if(!visibilityState){
        console.log("Tab Switch happened ...");
    }
});
Prakash Palnati
  • 3,231
  • 22
  • 35
Bikram
  • 323
  • 5
  • 10

1 Answers1

0

What's your target browser support? Have you tried looking into the Page Visibility API? Or if you're using jQuery you can do $(window).on("blur focus", someCallbackHere) like how it's demonstrated in this answer.

Rolando Cruz
  • 2,834
  • 1
  • 16
  • 24