简体   繁体   English

如何检测用户何时使用 javascript 切换选项卡?

[英]How can I detect when user switches the tab using javascript?

I want to end the fullscreen mode when the tab is changed or quickly switched.我想在标签更改或快速切换时结束全屏模式。 Is it possible to do it with javascript?是否可以用 javascript 做到这一点?

I have the following javascript code which ends fullscreen I want help in detecting the change in inter chrome tabs [using ctrl + tab shortcut] also in detecting the change in windows tabs like when a person switch chrome(any browser) to file explorer (for example)[using alt + tab shortcut].我有以下 javascript 代码结束全屏我需要帮助检测铬间标签的变化[使用 ctrl + 标签快捷方式] 也检测 windows 标签的变化,比如当一个人将 chrome(任何浏览器)切换到文件资源管理器(对于示例)[使用 alt + tab 快捷方式]。

My javascript code --我的 javascript 代码——

if (document.exitFullscreen) {
  document.exitFullscreen();
} 
else if (document.mozCancelFullScreen) {
  document.mozCancelFullScreen();
} 
else if (document.webkitExitFullscreen) {
  document.webkitExitFullscreen();
} 
else if (document.msExitFullscreen) {
  document.msExitFullscreen();
}

I want to wrap this code in the switch tab function.我想将此代码包装在开关选项卡 function 中。 Please help.请帮忙。

I also tried -我也试过——

document.addEventListener("visibilitychange", function tabsswitch() {
   if(document.hidden) {
     alert('document hidden')
   } 
});

but that's not working when i change the windows tab for instance like i switch from chrome to my code editor and again back to chrome it does't show any alert.但是当我更改 windows 选项卡时,这不起作用,例如我从 chrome 切换到我的代码编辑器,然后再回到 chrome,它没有显示任何警报。

There is HTML 5 API to detect if tab visibility is changed:有 HTML 5 API 来检测标签可见性是否改变:

document.addEventListener("visibilitychange", function () {});

The Page Visibility API provides us with two top-level attributes: document.hidden (boolean) and document.visibilityState (which could be any of these strings: “hidden”, “visible”, “prerender”, “unloaded”).页面可见性 API 为我们提供了两个顶级属性:document.hidden(布尔值)和 document.visibilityState(可以是以下字符串中的任何一个:“hidden”、“visible”、“prerender”、“unloaded”)。

This would not be not good enough without an event we could listen to though, that's why the API also provides the useful visibilitychange event.如果没有我们可以收听的事件,这还不够好,这就是 API 还提供有用的可见性更改事件的原因。

So, here's a basic example of how we could take action on a visibility change:因此,这是一个基本示例,说明我们如何对可见性更改采取行动:

function handleVisibilityChange() {
  if(document.hidden) {
    // the page is hidden
  } else {
   // the page is visible
  }
}

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

Reference 参考

I got similar issue, while using FullScreen mode tab visibility wasn't working.我遇到了类似的问题,而使用全屏模式选项卡可见性不起作用。 To detect the visibility while using alt + tab add this to existing code要在使用 alt + tab 时检测可见性,请将其添加到现有代码中

window.addEventListener('focus', function() {
  console.log('show');
}, false);
window.addEventListener('blur', function() {
   console.log('hide');
}, false);

this worked for me!这对我有用! Hope this will be useful to detect the tab change in full screen and while using alt + tab希望这对于在全屏和使用alt + tab时检测选项卡更改很有用

暂无
暂无

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

相关问题 Javascript-检测用户何时切换到另一个应用程序 - Javascript - Detect when user switches to another application 是否可以检测用户何时切换到其他浏览器选项卡? - Is it possible to detect when a user switches to a different browser tab? 当用户切换到选项卡集中由动态创建的选项卡时,如何调用javascript方法? - How to call a javascript method when user switches to dyanamically created tab in tabset? 如何在禁用Javascript时检测并警告用户? - How can I detect and warn the user when Javascript is disabled? 使用 Javascript,如何检测按键事件而不是用户在表单字段中键入时? - Using Javascript, how can I detect a keypress event but NOT when the user is typing in a form field? 如何使用Javascript检测用户是否在公司网络内? - How can I detect if a user is inside a corporate network using Javascript? 使用托管字段时,如何检测用户何时使用 Braintree JavaScript SDK 提交表单? - How can i detect when user submits the form when using Braintree JavaScript SDK when using hosted fields? 我如何检测用户何时使用jQuery在按“制表符”的输入之间进行切换? - How do I detect when the user changes between inputs pressing 'tab' using jQuery? 当焦点位于使用 JavaScript 的按钮上时如何检测“tab keypress” - How to detect "tab keypress" when focus is at a button using JavaScript 当用户切换标签时,如何允许Easeljs代码运行器保持活动状态 - How can I allow Easeljs Tickers to remain active when a user switches tabs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM