JavaScript:location.href 在新窗口/选项卡中打开?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5141910/
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
JavaScript: location.href to open in new window/tab?
提问by iamjonesy
I have a JavaScript file from a third party developer. It has a has link which replaces the current page with the target. I want to have this page opened in a new tab.
我有一个来自第三方开发人员的 JavaScript 文件。它有一个用目标替换当前页面的链接。我想在新选项卡中打开此页面。
This is what I have so far:
这是我到目前为止:
if (command == 'lightbox') {
location.href="https://support.wwf.org.uk/earth_hour/index.php?type=individual";
}
Can anyone help me out?
谁能帮我吗?
回答by alex
window.open(
'https://support.wwf.org.uk/earth_hour/index.php?type=individual',
'_blank' // <- This is what makes it open in a new window.
);
回答by andrew field
If you want to use location.href
to avoid popup problems, you can use an empty <a>
ref and then use javascript to click it.
如果你想用来location.href
避免弹出问题,你可以使用一个空的<a>
ref,然后使用 javascript 来单击它。
something like in HTML
类似于 HTML
<a id="anchorID" href="mynewurl" target="_blank"></a>
Then javascript click it as follows
然后javascript点击它如下
document.getElementById("anchorID").click();
回答by user616639
You can open it in a new window with window.open('https://support.wwf.org.uk/earth_hour/index.php?type=individual');
. If you want to open it in new tab open the current page in two tabs and then alllow the script to run so that both current page and the new page will be obtained.
您可以在新窗口中打开它window.open('https://support.wwf.org.uk/earth_hour/index.php?type=individual');
。如果要在新选项卡中打开它,请在两个选项卡中打开当前页面,然后允许脚本运行,以便同时获取当前页面和新页面。