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

后端 未结 2 1763
孤独总比滥情好
孤独总比滥情好 2020-12-21 00:07

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\";

  // S         


        
相关标签:
2条回答
  • 2020-12-21 00:43

    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.

    0 讨论(0)
  • 2020-12-21 01:06

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

    document.addEventListener("visibilitychange", handleVisibilityChange, false)
    
    0 讨论(0)
提交回复
热议问题