JavaScript:强制 IE 打开新标签页而不是窗口?

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

JavaScript: Forcing IE into opening new tab instead of window?

javascriptinternet-explorerwindow

提问by 12 secs ago

I want a script that makes a user's Internet Explorer open a new tab instead of a new window whenever someone clicks on a (target="_blank") link/banner on my webpage.

我想要一个脚本,当有人点击我网页上的 (target="_blank") 链接/横幅时,它会让用户的 Internet Explorer 打开一个新选项卡而不是一个新窗口。

I hate how Internet Explorer defaults to opening new windows instead of new tabs when a link/banner is clicked. All the other popular browsers (e.g. Chrome, Opera, Firefox) open tabs by default.

我讨厌 Internet Explorer 在单击链接/横幅时默认打开新窗口而不是新选项卡的方式。默认情况下,所有其他流行的浏览器(例如 Chrome、Opera、Firefox)都打开选项卡。

Preferably this script would need to work in all versions of IE that support tabs.

最好这个脚本需要在支持选项卡的所有版本的 IE 中工作。

So... How to, using JavaScript force IE into opening new tab instead of window, which is it's default behaviour?

那么...如何使用 JavaScript 强制 IE 打开新选项卡而不是窗口,这是默认行为?

回答by Jared Farrish

This isn't possible; it's a browser setting/behavior which is not callable from a script loaded on the page.

这是不可能的;这是一个浏览器设置/行为,不能从页面上加载的脚本中调用。

Programmatically open new pages on Tabs

以编程方式在选项卡上打开新页面

回答by John Rix

In IE11 at least, the default user setting is to 'Let Internet Explorer decide how popups should open' (Off-hand, I'm not sure what the default was in previous versions). Some context around the IE behaviour with this default setting can be found here: https://superuser.com/questions/713743/what-rules-apply-when-ie9-decides-how-to-open-popups

至少在 IE11 中,默认用户设置是“让 Internet Explorer 决定弹出窗口的打开方式”(顺便说一下,我不确定以前版本中的默认设置是什么)。可以在此处找到有关使用此默认设置的 IE 行为的一些上下文:https: //superuser.com/questions/713743/what-rules-apply-when-ie9-decides-how-to-open-popups

Per others' answers/comments here, generally speaking, we as web programmers have little control over how popups are opened. However, provided users have not changed their default selection (in IE11 at least!), you can approximate the behaviour you want through appropriate invocation of the window.open()method. Specifically, if you pass less than three arguments to the method, IE will generally open a new tab, while passing the specs(third) argument (even if with a null or undefined value <- this caught me out initially!) results in IE popping a separate window.

根据其他人的回答/评论,一般来说,我们作为 Web 程序员几乎无法控制弹出窗口的打开方式。但是,如果用户没有更改他们的默认选择(至少在 IE11 中!),您可以通过适当调用window.open()方法来近似您想要的行为。具体来说,如果您向该方法传递的参数少于三个,IE 通常会打开一个新选项卡,同时传递specs(第三个)参数(即使使用 null 或未定义的值 <- 这最初让我感到震惊!)导致 IE 弹出一个单独的窗口。

If you're happy to live with a solution that isn't guaranteed but works a fair amount of the time, this may help. The 'tyranny of the default' works in our favour in this instance at least!

如果您乐于接受无法保证但在相当长的时间内有效的解决方案,这可能会有所帮助。至少在这种情况下,“默认暴政”对我们有利!

Chrome at least is not quite so picky about the specsargument - if null or undefined, it will pop the tab instead of the window under default configuration.

Chrome 至少对这个specs参数不是那么挑剔- 如果为 null 或未定义,它将弹出选项卡而不是默认配置下的窗口。