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

左心房为你撑大大i 提交于 2020-02-27 04:23:08

我有来自第三方开发人员的JavaScript文件。 它具有一个具有链接的链接,该链接将当前页面替换为目标页面。 我想在新标签页中打开此页面。

这是我到目前为止的内容:

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

谁能帮我吗?


#1楼

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

类似HTML

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

然后用javascript单击它,如下所示

document.getElementById("anchorID").click();

#2楼

例如:

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

    });

工作示例


#3楼

window.open(
  'https://support.wwf.org.uk/earth_hour/index.php?type=individual',
  '_blank' // <- This is what makes it open in a new window.
);

#4楼

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!