简体   繁体   English

使用纯JS或jQuery检测浏览器页面窗口何时失去与Alt-Tab的焦点

[英]Detect when a browser page window loses focus with Alt-Tab using pure JS or jQuery

Is it possible to detect current page is in alt-tab? 是否可以检测当前页面在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. 但是此代码不会检测到浏览器的新窗口,也不会检测到Alt-tab进入任何其他程序。 Is it possible to detect it? 有可能检测到它吗? Or in jQuery? 还是在jQuery中?

EDIT New page means Ctrl(cmd)+N (new window) hotkey. 编辑新页面表示Ctrl(cmd)+ N(新窗口)热键。 The code above can not detect this. 上面的代码无法检测到这一点。 Alt(cmd)+tab to another program - impossible to detect too. Alt(cmd)+ tab到另一个程序-也无法检测到。 The code above can only detect Ctrl(cmd)+T (new tab) 上面的代码只能检测Ctrl(cmd)+ T(新标签)

EDIT I want to detect when a user return to my site from another application. 编辑我想检测用户何时从另一个应用程序返回我的网站。 That is, if a user closes any tab (eg, by Ctrl+W) and returns to my site I can detect this action using the script above. 也就是说,如果用户关闭任何选项卡(例如,通过Ctrl + W组合键)并返回到我的网站,则可以使用上面的脚本检测到此操作。 But if a user returns to my site from another application (eg, by Alt+Tab) the script doesn't work because window.onfocus will not be fired! 但是,如果用户从另一个应用程序(例如,通过Alt + Tab)返回我的网站,则该脚本将无法运行,因为window.onfocus将不会被触发! That is, 那是,

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

doesn't work for Alt+Tab action. 不适用于Alt + Tab动作。 Is it more clear? 更清楚了吗?

You can simply use the onfocus event on window , like in: 您可以简单地在window上使用onfocus事件,例如:

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

If needed, you can also use onblur for a more acute handling. 如果需要,还可以使用onblur进行更敏锐的处理。

有一个页面可见性API: https//developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API

document.addEventListener("visibilitychange", handleVisibilityChange, false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM