Html Javascript - 在新标签页中打开链接(相同窗口)

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

Javascript - Open Link in New Tab (SAME WINDOW)

javascripthtmlgoogle-chromefirefoxbrowser

提问by A.O.

I realize there are a couple questions already on SO for this topic, but they all seem to be quite old....just trying to get an up-to-date answer for this:

我意识到这个话题已经有几个关于 SO 的问题,但它们似乎都已经很老了……只是想得到一个最新的答案:

Is the standard way of opening a new tab (within the same browser window) still:

打开新选项卡(在同一浏览器窗口中)的标准方式仍然是:

window.open('url', '_blank');
window.focus();

???

???

Also, I've read that it is dependent on the users config of their browser (whether the new page opens in a new tab or a new window, and also whether the new tab/window gets the focus)....I would like the focus to remain on the original tab, but I am more concerned with it opening a tab in the same browser window (keeping focus is just a bonus).

另外,我已经读到它取决于用户浏览器的配置(新页面是在新选项卡还是新窗口中打开,以及新选项卡/窗口是否获得焦点)......我会就像焦点保持在原始选项卡上一样,但我更关心它在同一浏览器窗口中打开一个选项卡(保持焦点只是一个奖励)。

So is there a way to read/get this setting in new browsers? (chrome, ff, ie) And possibly notify the user to change their settings if they have it set to open in a new window?

那么有没有办法在新浏览器中读取/获取此设置?(chrome, ff, ie) 如果他们将设置设置为在新窗口中打开,可能会通知用户更改他们的设置?

回答by Giganticus

I have had great success with

我已经取得了巨大的成功

<a target='_blank' > 

回答by Mike Causer

Using target="_blank"is favourable.

使用target="_blank"是有利的。

eg. in Chrome, anchors with target="_blank"open a new tab, however, window.openopens a whole new window.

例如。在 Chrome 中,锚点target="_blank"打开一个新标签,然而,window.open打开一个全新的窗口。

I tried a few experiments to replace window.openwith target="_blank".

我尝试了一些实验,以取代window.opentarget="_blank"

Blocked by popup blocker

被弹出窗口拦截器拦截

// create an anchor, add to body, trigger click
var a = document.createElement('a');
a.setAttribute('href', 'http://google.com');
a.setAttribute('target', '_blank');
document.body.appendChild(a);
a.click();

// hiHyman first anchor, change href, trigger click
var a = document.getElementsByTagName('a')[0];
a.setAttribute('href', 'http://google.com');
a.setAttribute('target', '_blank');
a.click();

// hiHyman first anchor that has target=_blank, change href, trigger click
var a = $('a[target="_blank"]')[0];
a.setAttribute('href', 'http://google.com');
a.click();

Allowed by popup blocker

弹出窗口拦截器允许

// hiHyman first anchor that has target=_blank, change href, next document click triggers it
var a = $('a[target="_blank"]')[0];
a.setAttribute('href', 'http://google.com');
$(document).click(function(){
    $('a[target="_blank"]')[0].click();
});

// create an anchor, add to body, next document click triggers it
var a = document.createElement('a');
a.setAttribute('href', 'http://google.com');
a.setAttribute('target', '_blank');
document.body.appendChild(a);
$(document).click(function(){
    a.click();
});

It seems as long as the popups are triggered by a user interaction, the popup blocker allows it.

似乎只要弹出窗口是由用户交互触发的,弹出窗口阻止程序就允许它。

Mozilla's documentation on window.open:

Mozilla 的文档window.open

https://developer.mozilla.org/en-US/docs/Web/API/window.open

https://developer.mozilla.org/en-US/docs/Web/API/window.open