8

Is it possible to detect current page is in alt-tab? This code works only if a new tab in browser is opened:

(function() {
  var hidden = "hidden";

  // Standards:
  if (hidden in document)
    document.addEventListener("visibilitychange", onchange);
  else if ((hidden = "mozHidden") in document)
    document.addEventListener("mozvisibilitychange", onchange);
  else if ((hidden = "webkitHidden") in document)
    document.addEventListener("webkitvisibilitychange", onchange);
  else if ((hidden = "msHidden") in document)
    document.addEventListener("msvisibilitychange", onchange);
  // IE 9 and lower:
  else if ("onfocusin" in document)
    document.onfocusin = document.onfocusout = onchange;
  // All others:
  else
    window.onpageshow = window.onpagehide
    = window.onfocus = window.onblur = onchange;

  function onchange (evt) {
    var v = "visible", h = "hidden",
        evtMap = {
          focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h
        };

    evt = evt || window.event;
    if (evt.type in evtMap)
      document.body.className = evtMap[evt.type];
    else
      document.body.className = this[hidden] ? "hidden" : "visible";
  //console.log(this[hidden] ? "hidden" : "visible");
  }

  // set the initial state (but only if browser supports the Page Visibility API)
  if( document[hidden] !== undefined )
    onchange({type: document[hidden] ? "blur" : "focus"});
})();

But this code does detect neither new window of the browser nor alt-tab into any other programm. Is it possible to detect it? Or in jQuery?

EDIT New page means Ctrl(cmd)+N (new window) hotkey. The code above can not detect this. Alt(cmd)+tab to another program - impossible to detect too. The code above can only detect Ctrl(cmd)+T (new tab)

EDIT I want to detect when a user return to my site from another application. That is, if a user closes any tab (e.g., by Ctrl+W) and returns to my site I can detect this action using the script above. But if a user returns to my site from another application (e.g., by Alt+Tab) the script doesn't work because window.onfocus will not be fired! That is,

 window.onpageshow =
 window.onpagehide = window.onfocus = window.onblur 

doesn't work for Alt+Tab action. Is it more clear?

5
  • What does current page is an alt-tab mean?
    – jfriend00
    Dec 20, 2015 at 17:09
  • @jfriend00 , updated
    – Vyacheslav
    Dec 20, 2015 at 17:12
  • I'm still not clear on what you want, but what about using focusout event on the <body> element. (You'd have to use jQuery to support Firefox.) Dec 20, 2015 at 17:56
  • @StephenThomas, updated.
    – Vyacheslav
    Dec 20, 2015 at 18:55
  • While this solution may work on it's own, it fails when checking through an iframe because under certain circumstances the parent gets the focus instead of the frame, therefore none of the focus events get detected.
    – Ivan
    Jan 31, 2020 at 18:38

2 Answers 2

20

You can simply use the onfocus event on window, like in:

window.onfocus = function() {
  console.log('Got focus');
}

If needed, you can also use onblur for a more acute handling.

3
  • You was right. This is my fault. In the code above it was a wrong code. onfocus is fired on alt+tab.
    – Vyacheslav
    Dec 22, 2015 at 6:42
  • i notice that for me (latest Chrome) window.onfocus does not fire, while window.addEventListener does work.
    – Michael
    Aug 27, 2017 at 19:49
  • 2
    @Michael Strange: I just checked Chrome (after ensuring to get the latest version), and it works fine, both for onfocus and onblur.
    – cFreed
    Aug 28, 2017 at 10:18
-2

There is a Page Visibility API available: https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API

document.addEventListener("visibilitychange", handleVisibilityChange, false)
3
  • 5
    I don't think this API will help you when the focus goes to another window. Your page is hidden only if it's not the active browser tab, if the browser is minimized or if the OS screen is locked, AFAIK.
    – jods
    Dec 20, 2015 at 23:25
  • 3
    @kulak, If you look at my code you will see this event is not fired in alt+tab action
    – Vyacheslav
    Dec 21, 2015 at 8:10
  • @jods on Ubuntu OS Screen locking doesn't trigger the event. Minimization and tab-switching does work indeed. (Tested Chrome 67, Ubuntu 16.04.4)
    – Dimitry K
    Aug 2, 2018 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.