简体   繁体   English

如何将浏览器焦点从一个标签更改为另一个标签

[英]How to change browser focus from one tab to another

I have a JavaScript chat client running in one browser tab (call it tab A). 我有一个JavaScript聊天客户端在一个浏览器选项卡中运行(称之为选项卡A)。 Now when a message arrives the user might be on another browser tab (call it tab B). 现在,当消息到达时,用户可能在另一个浏览器选项卡上(称为选项卡B)。 I'm looking for ways to change the focus from tab B to my chat client (tab A) when such a message arrives. 当这样的消息到来时,我正在寻找将焦点从标签B更改为聊天客户端(标签A)的方法。

I could not find a way to do this. 我找不到办法做到这一点。

It is not possible - due to security concerns. 这是不可能的-出于安全考虑。

unless by "tab" you mean a window and a popup window that (due to browser preferences) opened up in a new tab. 除非 “tab”表示一个窗口和一个弹出窗口(由于浏览器首选项)在新选项卡中打开。 If this is the case, then yes you can. 如果是这种情况,那么你可以。

//focus opener... from popup
window.opener.focus();

//focus popup... from opener
yourPopupName.focus(); 

It is possible to shift focus back to Tab A by means of an alert in Tab A eg alert('New Message') 可以通过标签A中的警报将焦点移回标签A,例如警报('新消息')

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. 否则,更新标签A的标题和/或图标似乎是最好的,因为nc3b说。

你最好的可能是改变页面的标题,提醒用户选项卡需要注意(也许还有favicon - 看看meebo是怎么做的,它真的很讨厌但很有效)

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(如果更改则调用new),然后继续提交表单。

  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> 

Chrome and firefox now have notifications. Chrome和Firefox现在都有通知。 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. 我认为通知可能是一种更加用户友好的方式来提醒用户您的应用上发生了某些变化,而不是弹出警报并强制他们进入您的页面。

Using Javascript, triggering an alert can have the desired effect. 使用Javascript,触发警报可以产生预期的效果。 Run this code in your console, or add to your html file in one tab and switch to another tab in the same browser. 在控制台中运行此代码,或在一个选项卡中添加到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 ). 但是,出于UX原因,您仍然可以在页面标题((1)Facebook)中使用Facebook的消息计数器中的ping或添加和指示符。 You can also experiment with Notifications API (experimental). 您还可以尝试使用Notifications API (实验性)。

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

相关问题 如何将Tab键从一个元素聚焦到另一个元素? - How to focus of tab from one element to another? 如何使用jQuery将焦点从一个文本框更改为另一个文本框? - How to change focus from one textbox to another using jQuery? 如何从另一个标签中选择浏览器标签? - How to select a browser tab from another tab? 如何检查用户是在一个选项卡上登录该站点而不是在同一浏览器中的另一个选项卡上? - How can you check if a user is logged into the site on one tab but not on another tab from the same browser? 如何从选项卡内部更改为另一个选项卡? - How to change to another tab from inside of a tab? 从内部将焦点设置到“浏览器”选项卡 - Set focus to Browser tab from the inside 如何以角度将数据从一个组件传递到另一个组件(新浏览器选项卡)? - How do I pass data from one component to another (New Browser Tab) in angular? 如果对话框在一个浏览器选项卡中打开,是否阻止它在另一个浏览器选项卡中打开? - If dialog opens in one browser tab, prevent it from opening in another? 在没有浏览器扩展的情况下将内容从一个选项卡传输到另一个选项卡? - Transfer content from one tab to another without browser extension possible? 将键盘和鼠标事件从一个浏览器选项卡发送到另一个 - Sending keyboard and mouse events from one browser tab to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM