JavaScript:location.href是否在新窗口/选项卡中打开?

JavaScript:location.href是否在新窗口/选项卡中打开?,javascript,window.open,window.location,Javascript,Window.open,Window.location,我有一个来自第三方开发人员的JavaScript文件。它有一个链接,可以用目标替换当前页面。我想在新选项卡中打开此页 这就是我到目前为止所做的: if (command == 'lightbox') { location.href="https://support.wwf.org.uk/earth_hour/index.php?type=individual"; } 有人能帮我吗?window.open( window.open( 'https://support.wwf.org.uk/

我有一个来自第三方开发人员的JavaScript文件。它有一个链接,可以用目标替换当前页面。我想在新选项卡中打开此页

这就是我到目前为止所做的:

if (command == 'lightbox') {
 location.href="https://support.wwf.org.uk/earth_hour/index.php?type=individual";
}
有人能帮我吗?

window.open(
window.open(
  'https://support.wwf.org.uk/earth_hour/index.php?type=individual',
  '_blank' // <- This is what makes it open in a new window.
);
'https://support.wwf.org.uk/earth_hour/index.php?type=individual',
“_blank”/您可以使用
window.open('https://support.wwf.org.uk/earth_hour/index.php?type=individual')
。如果要在“新建”选项卡中打开它,请在两个选项卡中打开当前页面,然后全部关闭要运行的脚本,以便同时获得当前页面和新页面。

如果要使用
location.href
避免弹出问题,可以使用空的
ref,然后使用javascript单击它

类似HTML的东西

<a id="anchorID" href="mynewurl" target="_blank"></a>
例如:

    $(document).on('click','span.external-link',function(){
        var t               = $(this), 
            URL             = t.attr('data-href');        
        $('<a href="'+ URL +'" target="_blank">External Link</a>')[0].click();

    });
$(文档).on('click','span.external link',function()){
var t=$(此),
URL=t.attr('data-href');
$(“”)[0]。单击();
});

正在工作。

纯js替代window.open

let a= document.createElement('a');
a.target= '_blank';
a.href= 'https://support.wwf.org.uk/';
a.click();

这里正在工作(stackoverflow代码段不允许打开)

您还可以打开一个新选项卡,使用如下参数调用操作方法:

   var reportDate = $("#inputDateId").val();
   var url = '@Url.Action("PrintIndex", "Callers", new {dateRequested = "findme"})';
   window.open(window.location.href = url.replace('findme', reportDate), '_blank');

使用location.href将用新url替换当前url,即在同一网页中

要打开新选项卡,您可以按如下方式使用: 如果(命令=='lightbox'){ 窗口打开(“https://support.wwf.org.uk/earth_hour/index.php?type=individual“,”空白“);
}

值得一提的是:是否创建新选项卡或窗口取决于浏览器(设置)。@alex我编辑此答案是为了使重要的部分可读。如果您不同意我的格式,我可以建议缩短URL,以便第二个参数适合于展开区域吗?@ErickBest不正确。
“\u blank”
保证窗口/选项卡是新的。除其他特殊名称外,还有其他内容吗正在为该窗口指定特定名称,并且指向该目标的后续链接将重用该窗口。这在现代浏览器中只会触发弹出框通知,根本不会模拟“空白”锚定单击。浏览器阻止弹出框。我对该解决方案抱有希望,但它似乎仍然会触发弹出框(至少在Chrome中是这样)。我感觉浏览器意识到这是一个javascript点击,并以不同的方式对待它。谢谢你Nathan。你说得很对。你仍然会收到一条弹出框消息。我认为我已经解决了它。理论上它应该可以工作。Andrew
$(“#anchorID”)[0]。点击();
要在jquery中使用此解决方案。Lol theBell,如果您直接访问DOM元素,则它不是jquery
[0]
浏览器阻止弹出。这意味着您有一个adblock或其他阻止它的东西。默认行为应该在正常条件下工作。需要第一行和第二行吗?如果没有
reportDate
url
变量,它不会工作吗?您可以去掉参数(reportDate),但url变量很重要,因为它包含对action方法的调用。
   var reportDate = $("#inputDateId").val();
   var url = '@Url.Action("PrintIndex", "Callers", new {dateRequested = "findme"})';
   window.open(window.location.href = url.replace('findme', reportDate), '_blank');