如何在new选项卡(而不是new window)中打开链接?(复制)

时间:2022-01-07 21:17:13

This question already has an answer here:

这个问题已经有了答案:

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.

当使用简单的与target='blank',链接被打开在一个新的标签。

Is there a solution?

有解决方案吗?

7 个解决方案

#1


65  

CSS3 supports "open in new tab", by the property target-new

CSS3支持“在新标签中打开”,由属性target-new

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 this method to resize window immediately, to ensure that popup blockers do not kill your popup

另外,请立即使用此方法调整窗口大小,以确保弹出窗口拦截器不会杀死您的弹出窗口。

#2


9  

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目标新选项@anirudh4444之外,您不能也最重要的是不应该这样做。您试图控制用户的体验,而这很可能是由用户决定的。

#3


6  

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个不同的浏览器中对它们进行了测试。(谷歌Chrome, Mozilla Firefox, Microsoft Internet Explorer, Opera, K-meleon*和Seamonkey。)

  1. <a href="blaah" target="_blank">Blaah</a>
  2. 目标= < a href = " blaah " "平等" > blaah < / >
  3. <a href="blaah" target="_tab">Blaah</a>
  4. < a href = " blaah "目标= " _tab " > blaah < / >
  5. <a href="blaah" target="_new">Blaah</a>
  6. < a href = " blaah "目标= " _new " > blaah < / >

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,因为某些原因,打开链接时打开了页面。

#4


3  

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- tabin -browsers-instead of-windows/

Or see this JSFiddle

或者看到这个JSFiddle

#5


1  

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"(包括一个前导下划线),而您的简单链接有目标='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.

每个用户都可以最终控制弹出窗口是否应该打开新的窗口或新的选项卡。对此你无能为力。

#6


0  

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用户操作

#7


-3  

In case of a link yes only using target tag would work.

如果链接是yes,那么只使用目标标签就可以了。

But In case of a button try doing this with the onclick function 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

使用窗口。开放(url,目标)功能——它需要一个url,而不是和一个目标窗口名称,行为目标=一样“东西”属性链接....像这样

button(type="button", onclick="window.open('auth/google', '_blank');")

This should work.

这应该工作。

#1


65  

CSS3 supports "open in new tab", by the property target-new

CSS3支持“在新标签中打开”,由属性target-new

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 this method to resize window immediately, to ensure that popup blockers do not kill your popup

另外,请立即使用此方法调整窗口大小,以确保弹出窗口拦截器不会杀死您的弹出窗口。

#2


9  

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目标新选项@anirudh4444之外,您不能也最重要的是不应该这样做。您试图控制用户的体验,而这很可能是由用户决定的。

#3


6  

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个不同的浏览器中对它们进行了测试。(谷歌Chrome, Mozilla Firefox, Microsoft Internet Explorer, Opera, K-meleon*和Seamonkey。)

  1. <a href="blaah" target="_blank">Blaah</a>
  2. 目标= < a href = " blaah " "平等" > blaah < / >
  3. <a href="blaah" target="_tab">Blaah</a>
  4. < a href = " blaah "目标= " _tab " > blaah < / >
  5. <a href="blaah" target="_new">Blaah</a>
  6. < a href = " blaah "目标= " _new " > blaah < / >

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,因为某些原因,打开链接时打开了页面。

#4


3  

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- tabin -browsers-instead of-windows/

Or see this JSFiddle

或者看到这个JSFiddle

#5


1  

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"(包括一个前导下划线),而您的简单链接有目标='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.

每个用户都可以最终控制弹出窗口是否应该打开新的窗口或新的选项卡。对此你无能为力。

#6


0  

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用户操作

#7


-3  

In case of a link yes only using target tag would work.

如果链接是yes,那么只使用目标标签就可以了。

But In case of a button try doing this with the onclick function 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

使用窗口。开放(url,目标)功能——它需要一个url,而不是和一个目标窗口名称,行为目标=一样“东西”属性链接....像这样

button(type="button", onclick="window.open('auth/google', '_blank');")

This should work.

这应该工作。