[英]Event for when user switches browser tabs
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 window.onblur
和window.onfocus
似乎无法在所有浏览器中正常工作
Is there a proxy I could look at in order to synthesize this event?是否有我可以查看的代理来合成此事件?
You might try using a framework, such as MooTools or jQuery which provide cross-browser support.您可以尝试使用提供跨浏览器支持的框架,例如 MooTools 或 jQuery。 They should be able to detect with more reliability the
blur
and focus
events for the browser window.他们应该能够更可靠地检测浏览器窗口的
blur
和focus
事件。
I personally have used jQuery with much success:我个人使用 jQuery 非常成功:
$(window).blur(function(e) {
// Do Blur Actions Here
});
$(window).focus(function(e) {
// Do Focus Actions Here
});
You can also try and use VisibilityAPI .您也可以尝试使用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) 另请参阅 Stackoverflow 上的此处(可能重复)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.