简体   繁体   English

用户切换浏览器标签时的事件

[英]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.onblurwindow.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.他们应该能够更可靠地检测浏览器窗口的blurfocus事件。

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.

相关问题 是否可以检测用户何时切换到其他浏览器选项卡? - Is it possible to detect when a user switches to a different browser tab? 当用户切换标签时,如何允许Easeljs代码运行器保持活动状态 - How can I allow Easeljs Tickers to remain active when a user switches tabs 当用户切换选项卡时,使用setInterval淡化内容无法正常工作 - Fading content using setInterval doesn't work properly when user switches tabs 在Internet Explorer中打开多个选项卡时检测浏览器关闭事件 - Detect browser close event when multiple tabs are open in Internet Explorer 当用户切换到完全不同的 window 时,是否有任何方法可以避免聚焦事件? - Is there any way to avoid a focusout event when the user switches to an entirely different window? 页面切换时更新事件监听器 - Update event listeners when page switches 用户在浏览器中更改字体大小时的事件? - Event when the user changes font size in browser? Javascript-检测用户何时切换到另一个应用程序 - Javascript - Detect when user switches to another application 如何向在socket.io中的不同选项卡或浏览器中打开的同一用户发出事件? - How to emit event to same user which is opened in different tabs or browser in socket.io? JavaScript 浏览器关闭事件(nob 选项卡) - JavaScript browser close event (nob tabs)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM