Event for when user switches browser tabs

前端 未结 2 826
深忆病人
深忆病人 2020-11-28 08:23

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

2条回答
  •  攒了一身酷
    2020-11-28 09:06

    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
    });
    

提交回复
热议问题