Javascript 如何将浏览器焦点从一个选项卡更改为另一个选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2704206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change browser focus from one tab to another
提问by Max
I have a JavaScript chat client running in one browser tab (call it tab A). Now when a message arrives the user might be on another browser tab (call it tab B). I'm looking for ways to change the focus from tab B to my chat client (tab A) when such a message arrives.
我在一个浏览器选项卡(称为选项卡 A)中运行了一个 JavaScript 聊天客户端。现在,当消息到达时,用户可能在另一个浏览器选项卡上(称为选项卡 B)。当这样的消息到达时,我正在寻找将焦点从选项卡 B 更改为我的聊天客户端(选项卡 A)的方法。
I could not find a way to do this.
我找不到办法做到这一点。
回答by scunliffe
It is notpossible - due to security concerns.
这是不是可能-出于安全考虑。
unlessby "tab" you mean a window and a popup window that (due to browser preferences) opened up in a new tab. If this is the case, then yes you can.
除非“选项卡”是指在新选项卡中打开的窗口和弹出窗口(由于浏览器首选项)。如果是这种情况,那么是的,您可以。
//focus opener... from popup
window.opener.focus();
//focus popup... from opener
yourPopupName.focus();
回答by Autoabacus
It is possible to shift focus back to Tab A by means of an alert in Tab A e.g. alert('New Message')
可以通过 Tab A 中的警报将焦点移回 Tab A,例如 alert('New Message')
However, you need to be careful using this as it is very likely to annoy people. You should only use it if you make it optional in your app. Otherwise, updating Tab A's title and/or the favicon would appear to be best as nc3b says.
但是,您需要小心使用它,因为它很可能会惹恼人们。仅当您在应用中将其设为可选时才应使用它。否则,如 nc3b 所说,更新选项卡 A 的标题和/或网站图标似乎是最好的。
回答by nc3b
The best you could would probably be to change the title of the page alerting the user the tab needs attention (maybe also the favicon - look at how meebo does it, it's really annoying but effective)
最好的方法可能是更改页面标题,提醒用户该选项卡需要注意(也许还有 favicon - 看看 meebo 是如何做到的,这真的很烦人但很有效)
回答by CallMeNorm
Chrome and firefox now have notifications. I think notifications are probably a more user friendly way to alert the user that something has changed on your app than popping an alert and forcing them to your page.
Chrome 和 firefox 现在有通知。我认为通知可能是一种更用户友好的方式来提醒用户您的应用程序发生了某些变化,而不是弹出警报并强制他们进入您的页面。
回答by Artistan
this worked for me on form submit to reopen the target window.. so it will call window.open on the same target (or new if changed) and then continue to submit the form.
这对我在表单提交上工作以重新打开目标窗口..所以它会在同一个目标上调用 window.open (或新的,如果更改),然后继续提交表单。
var open_target = function (form){
var windowName = jQuery(form).attr('target');
window.open("", windowName );
return true;
};
<form target="_search_elastic" onsubmit="return open_target(this);">
</form>
回答by William
Using Javascript, triggering an alert can have the desired effect. Run this code in your console, or add to your html file in one tab and switch to another tab in the same browser.
使用 Javascript,触发警报可以达到预期的效果。在您的控制台中运行此代码,或在一个选项卡中添加到您的 html 文件并在同一浏览器中切换到另一个选项卡。
setTimeout(function(){
alert("Switched tabs");
},
5000);
The alert appearing after the timeout will trigger tab switch. Or you can do something similar! For UX reasons however, you can still use a ping or add and indicator like in Facebook's message counter in the page title ( (1) Facebook ). You can also experiment with Notifications API(experimental).
超时后出现的警报将触发选项卡切换。或者你可以做类似的事情!但是,出于 UX 原因,您仍然可以在页面标题 ((1) Facebook 中的 Facebook 消息计数器中使用 ping 或添加和指示器)。您还可以试验Notifications API(实验性)。

