456

我有一个来自第三方开发者的 JavaScript 文件。它有一个链接,用目标替换当前页面。我想在新标签页中打开这个页面。

这是我到目前为止所拥有的:

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

谁能帮我吗?

4

7 回答 7

1126
window.open(
  'https://support.wwf.org.uk/earth_hour/index.php?type=individual',
  '_blank' // <- This is what makes it open in a new window.
);
于 2011-02-28T12:24:20.817 回答
35

如果你想用它location.href来避免弹出问题,你可以使用一个空的<a>ref 然后使用 javascript 来点击它。

类似于 HTML 的东西

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

然后javascript点击如下

document.getElementById("anchorID").click();
于 2014-08-15T14:50:53.027 回答
31

纯js替代window.open

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

这是工作示例(stackoverflow 片段不允许打开)

于 2019-12-05T19:02:35.323 回答
11

您可以在新窗口中使用window.open('https://support.wwf.org.uk/earth_hour/index.php?type=individual');. 如果要在新选项卡中打开它,请在两个选项卡中打开当前页面,然后允许脚本运行,以便获取当前页面和新页面。

于 2011-02-28T12:28:29.723 回答
0

例如:

    $(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();

    });

工作示例

于 2018-05-13T13:34:36.967 回答
0

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

   var reportDate = $("#inputDateId").val();
   var url = '@Url.Action("PrintIndex", "Callers", new {dateRequested = "findme"})';
   window.open(window.location.href = url.replace('findme', reportDate), '_blank');
于 2020-06-04T20:32:03.743 回答
0

使用 location.href 将用新的 url 替换当前 url,即https://support.wwf.org.uk/earth_hour/index.php?type=individual在同一网页中。

要打开一个新标签,您可以使用如下方式: if (command == 'lightbox') { window.open("https://support.wwf.org.uk/earth_hour/index.php?type=individual", ' _空白的'); }

于 2021-06-08T06:36:05.483 回答