Javascript 如何在新选项卡(而不是新窗口)中打开链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6296013/
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 can I open a link in new tab (and not new window)?
提问by Erik Sapir
I have an 'Open' button that 'calculates' a link that should be opened in a new tab. I try to use:
我有一个“打开”按钮,它“计算”应该在新选项卡中打开的链接。我尝试使用:
window.open(url, '_blank');
But that opens a new window.
但这会打开一个新窗口。
I also tried the following method:
我还尝试了以下方法:
<form id="oform" name="oform" action="" method="post" target="_blank">
</form>
...
document.getElementById("oform").action = url;
document.getElementById("oform").submit();
Still, a new window is opened, instead of a new tab.
尽管如此,还是会打开一个新窗口,而不是一个新选项卡。
When using simple <a href...>
with target='blank'
, the link is opened in a new tab.
使用 simple <a href...>
with 时target='blank'
,链接将在新选项卡中打开。
Is there a solution?
有解决办法吗?
回答by Anirudh Ramanathan
Update [2019] Most browsers today open in a new tab when you set the target to _blank
. The days of popup windows is long gone. We can now use:
更新 [ 2019] 当您将目标设置为_blank
. 弹出窗口的日子早已一去不复返了。我们现在可以使用:
<a href="some url" target="_blank">content of the anchor</a>
Most sane browsers will open the new window in a new tab.
大多数理智的浏览器会在新选项卡中打开新窗口。
CSS3 supports "open in new tab", by the property target-new
CSS3 通过属性target-new支持“在新标签页中打开”
target-new: window | tab | none;
target-new: window | tab | none;
Update [2016]: this method never made it into the CSS3 spec, as one of the comments indicates. This shouldn't be used. However, it can be seen that most modern browsers open target='_blank'
links in a new tab anyway, unless one attempts to resize the new tab immediately thereafter. However, there does not appear to be a mechanism to force this behavior in the specifications.
更新 [ 2016]:正如其中一条评论所示,此方法从未进入 CSS3 规范。这不应该被使用。但是,可以看出大多数现代浏览器target='_blank'
无论如何都会在新选项卡中打开链接,除非此后立即尝试调整新选项卡的大小。但是,在规范中似乎没有强制这种行为的机制。
[2011] For a method of forcing opening in a new tab that is well supported, try the following:
[ 2011] 对于在受支持的新选项卡中强制打开的方法,请尝试以下操作:
<a href="some url" target="_newtab">content of the anchor</a>
Else, use thismethod to resize window immediately, to ensure that popup blockers do not kill your popup
否则,使用此方法立即调整窗口大小,以确保弹出窗口阻止程序不会杀死您的弹出窗口
回答by Kon
Other than the CSS3 target-new option @anirudh4444 mentioned, you can't and mostly importantly probably shouldn't. You are trying to control the user's experience, when this should most likely be left up to the user.
除了提到的 CSS3 target-new 选项 @anirudh4444 之外,你不能,最重要的是可能不应该。您正在尝试控制用户的体验,而这很可能由用户决定。
回答by Ben
You can use any of the following, I tested them all in 6 different browsers. (Google Chrome, Mozilla Firefox, Microsoft Internet Explorer, Opera, K-meleon* and Seamonkey.)
您可以使用以下任何一种,我在 6 种不同的浏览器中测试了它们。(Google Chrome、Mozilla Firefox、Microsoft Internet Explorer、Opera、K-meleon* 和 Seamonkey。)
<a href="blaah" target="_blank">Blaah</a>
<a href="blaah" target="_tab">Blaah</a>
<a href="blaah" target="_new">Blaah</a>
<a href="blaah" target="_blank">Blaah</a>
<a href="blaah" target="_tab">Blaah</a>
<a href="blaah" target="_new">Blaah</a>
They all work the exact same, and the choice is completely up to preference.
它们的工作原理完全相同,选择完全取决于偏好。
*K-meleon, for some reason just opened up the page I was on when I clicked the link.
*K-meleon,出于某种原因,当我点击链接时刚刚打开了我所在的页面。
回答by Mukesh Agarwal
I was able to solve this issue using a "form" element.
我能够使用“表单”元素解决这个问题。
function openNewTab(link) {
var frm = $('<form method="get" action="' + link + '" target="_blank"></form>')
$("body").append(frm);
frm.submit().remove();
}
For complete implementation and details visit my post http://mukesh.in/force-open-new-tab-in-browsers-instead-of-windows/
有关完整的实施和详细信息,请访问我的帖子http://mukesh.in/force-open-new-tab-in-browsers-instead-of-windows/
Or see this JSFiddle
或者看这个 JSFiddle
回答by James Alarie
Your form has target="_blank" (including a leading underscore) while your simple link has the target='blank' without the leading underscore. The "_blank" is a reserved word specifying a particular action, but "blank" is the name of a specific, possibly new, window. That's why you're getting different results. Both are pop-ups, but different types.
您的表单具有 target="_blank"(包括前导下划线),而您的简单链接的 target='blank' 没有前导下划线。“_blank”是指定特定操作的保留字,但“blank”是特定窗口的名称,可能是新窗口。这就是为什么你会得到不同的结果。两者都是弹出窗口,但类型不同。
Each user has ultimate control about whether a pop-up should open a new window or a new tab. There's nothing you can do about it.
每个用户都可以最终控制弹出窗口是打开新窗口还是新选项卡。你对此无能为力。
回答by psema4
The following works in Chrome:
以下在 Chrome 中有效:
<script>
function buildAndGo() {
var aEl = document.getElementById('missingLink');
aEl.href = "#" + resultOfSomeCalculation();
var e = document.createEvent('MouseEvents');
e.initEvent( 'click', true, true );
aEl.dispatchEvent(e);
}
</script>
...
<button onclick="buildAndGo()">Do it</button>
<a href="#" id="missingLink" target="_blank" style="visibility: hidden;"></a>
Working from the following sources to try and get an IE version working: Selenium BrowserBot, YUI User Action
从以下来源工作以尝试使 IE 版本正常工作: Selenium BrowserBot、 YUI 用户操作
回答by Nikhil Nanjappa
In case of a link yes only using targettag would work.
在链接的情况下,只有使用目标标签才能工作。
But In case of a button try doing this with the onclickfunction instead of using just _blank
但是如果是按钮,请尝试使用onclick函数执行此操作,而不是仅使用_blank
Use the window.open(url, target) function instead - it takes a URL, and a target window name, which behaves the same as the target="something" attribute on a link....like this
改用 window.open(url, target) 函数 - 它需要一个 URL 和一个目标窗口名称,其行为与链接上的 target="something" 属性相同....像这样
button(type="button", onclick="window.open('auth/google', '_blank');")
This should work.
这应该有效。