56

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?

Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
EoghanM
  • 25,161
  • 23
  • 90
  • 123
  • I'm pretty sure this isn't possible, at least across all browsers. – Ian Elliott Jun 24 '09 at 14:20
  • Actually, cross-browser compatibility [does not seem that bad](http://www.quirksmode.org/dom/events/blurfocus.html). 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_. – lanzz Sep 09 '12 at 14:16
  • 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. – John Mar 26 '21 at 02:23

2 Answers2

69

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)

Peter
  • 37
  • 5
ronapelbaum
  • 1,617
  • 14
  • 19
48

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
});
DanO
  • 10,230
  • 4
  • 41
  • 34
  • Cool, I might take a look at how those are implemented in jQuery – EoghanM Jun 30 '09 at 10:27
  • @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. – RONE Jul 25 '13 at 06:45
  • 3
    _5 years later:_ Thanks Dude! – MCTaylor17 Dec 18 '14 at 08:22
  • 1
    Down-voted for answering a JavaScript question with jQuery. – John Mar 26 '21 at 01:48
  • @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. – Chris May 14 '23 at 20:59