Event for when user switches browser tabs

cross-browser, javascript

Solution

You can also try and use VisibilityAPI.

document.addEventListener("visibilitychange", function() {
    if (document.hidden){
        console.log("Browser tab is hidden")
    } else {
        console.log("Browser tab is visible")
    }
});

See also here on Stackoverflow (possible duplicate)

Problem

I'm looking for an event which will fire whenever the user switches away from the page to another tab, and another event which fires when the user switches back to the tab again. `window.onblur` and `window.onfocus` don't seem to work correctly across all browsers Is there a proxy I could look at in order to synthesize this event?

Original source

Related problems