Event for when user switches browser tabs
Asked Answered
I

2

61

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?

Immerge answered 24/6, 2009 at 14:12 Comment(3)
I'm pretty sure this isn't possible, at least across all browsers.Bane
Actually, cross-browser compatibility does not seem that bad. You get some doubled events with Firefox and Safari/Windows, but that should be fairly easy to work around. window.onfocus/onblur have been available since before the Browser Wars, and their behavior hasn't changed much. Apparently there are some bugs in implementations, but no differing semantics.Bowie
A lot of times I use autoscroll (middle mouse button) (on Windows at least) and a lot of copy-paste script-kiddies (not sure if it's window.onblur offhand) will trigger an obnoxious email subscription modal. Any developer worth their weight in mulch should test to make sure they're not annoying their users.Carden
C
75

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)

Centime answered 9/2, 2017 at 10:26 Comment(3)
VisibilityAPI is not good enough for all the browsers. It is not working on Firefox.Dupleix
@CodingbyRaj surely that's why its documentation page is hosted on mozilla (irony)Saracen
OP here: this is what I've been using for the last few years so have updated this to be the accepted answer.Immerge
E
49

You might try using a framework, such as MooTools or jQuery which provide cross-browser support. They should be able to detect with more reliability the blur and focus events for the browser window.

I personally have used jQuery with much success:

$(window).blur(function(e) {
    // Do Blur Actions Here
});
$(window).focus(function(e) {
    // Do Focus Actions Here
});
Epiphyte answered 24/6, 2009 at 14:42 Comment(4)
Cool, I might take a look at how those are implemented in jQueryImmerge
@Daniel Hey, actually i was looking for a similar kind of functionality. $(window).focus(function(e) { // Do Focus Actions Here}); The content inside focus will run every time when there is some other ajax functionality in the page, But one small change, 1. Is it possible to run the code inside this ONLY ONCE, when the user navigates back and forth between the same page.Negrillo
Down-voted for answering a JavaScript question with jQuery.Carden
@John, maybe I missed something but where did the OP specifically specify JavaScript? Yes, he tagged it JavaScript but he certainly did not indicate he would reject non-JavaScript solutions.Statampere

© 2022 - 2024 — McMap. All rights reserved.