Javascript - 如何让浏览器切换标签页?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4123950/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:26:37  来源:igfitidea点击:

Javascript - How to make a browser switch tabs?

javascriptjquery

提问by Ash Burlaczenko

In my code a timed event launches a ModalDialog. If the user switches tabs before the event the tab blinks but that is it.

在我的代码中,一个定时事件会启动一个 ModalDialog。如果用户在事件之前切换选项卡,选项卡会闪烁,但仅此而已。

Is there a way to make the browser switch to the tab the dialog is on?

有没有办法让浏览器切换到对话框所在的选项卡?

Thank you

谢谢

Cheers for all your advice. I should of said before but code will only be run through IE and the users are not allowed to alter any settings.

为您的所有建议干杯。我之前应该说过,但代码只能通过 IE 运行,并且不允许用户更改任何设置。

回答by Nick Craver

There isn't a cross-browser way do focus a tab/window that's 100% reliable, especially in newer browsers. This would be an advertisers dream...so it's actively blockedin most newer browsers.

没有一种跨浏览器的方式可以聚焦 100% 可靠的选项卡/窗口,尤其是在较新的浏览器中。这将是广告商的梦想……因此它在大多数较新的浏览器中都被主动阻止

As a user, I'm glad it's difficult in most cases, impossible in others to do this.

作为用户,我很高兴在大多数情况下很难做到这一点,而在其他情况下则不可能。

回答by Pointy

There is no reliable way to do that. The Internet Explorers are the most cooperative about raising windows (which is what switching tabs amounts to), while Safari pretty much ignores all such requests. Firefox can be configured (by its user, not by your code) to allow windows to grab focus, but it's rare for anybody to do that.

没有可靠的方法可以做到这一点。Internet Explorer 在提升窗口(这就是切换选项卡的意义)方面最为合作,而 Safari 几乎忽略了所有此类请求。Firefox 可以配置(由其用户,而不是您的代码)以允许窗口获取焦点,但很少有人这样做。

You can have your code call window.focus()but don't rely on it to work.

您可以调用代码,window.focus()但不要依赖它来工作。