Javascript - 通过单击按钮在新选项卡中打开给定的 URL

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

Javascript - Open a given URL in a new tab by clicking a button

javascript

提问by wong2

I would like to have a button that redirects to a given URL and opens in a new tab. How can this be done?

我想要一个重定向到给定 URL 并在新选项卡中打开的按钮。如何才能做到这一点?

回答by Luke Alderton

Use this:

用这个:

<input type="button" value="button name" onclick="window.open('http://www.website.com/page')" />

Worked for me and it will open an actual new 'popup' window rather than a new full browser or tab. You can also add variables to it to stop it from showing specific browser traits as follows:

为我工作,它将打开一个实际的新“弹出”窗口,而不是一个新的完整浏览器或选项卡。您还可以向其添加变量以阻止其显示特定的浏览器特征,如下所示:

onclick="window.open(this.href,'popUpWindow','height=400,width=600,left=10,top=10,,scrollbars=yes,menubar=no'); return false;"

回答by satnam

In javascript you can do:

在 javascript 中,您可以执行以下操作:

window.open(url, "_blank");

回答by Tim Büthe

Adding target="_blank" should do it:

添加 target="_blank" 应该这样做:

<a id="myLink" href="www.google.com" target="_blank">google</a>

回答by Tony

You can forget about using JavaScript because the browser controls whether or not it opens in a new tab. Your best option is to do something like the following instead:

您可以忘记使用 JavaScript,因为浏览器会控制它是否在新选项卡中打开。您最好的选择是执行以下操作:

<form action="http://www.yoursite.com/dosomething" method="get" target="_blank">
    <input name="dynamicParam1" type="text"/>
    <input name="dynamicParam2" type="text" />
    <input type="submit" value="submit" />
</form>

This will always open in a new tab regardless of which browser a client uses due to the target="_blank"attribute.

由于该target="_blank"属性,无论客户端使用哪种浏览器,这都将始终在新选项卡中打开。

If all you need is to redirect with no dynamic parameters you can use a link with the target="_blank"attribute as Tim Büthe suggests.

如果您只需要在没有动态参数的情况下重定向,您可以使用具有target="_blank"Tim Büthe 建议的属性的链接。

回答by James Allardice

Use window.openinstead of window.locationto open a new window or tab (depending on browser settings).

使用window.open而不是window.location打开新窗口或选项卡(取决于浏览器设置)。

Your fiddle does not work because there is no buttonelement to select. Try input[type=button]or give the button an idand use #buttonId.

您的小提琴不起作用,因为没有button要选择的元素。尝试input[type=button]或给按钮一个id并使用#buttonId

回答by SpyderScript

Old question, but I wanted to share my preferred method, which has the advantage of no nasty javascript being embedded in your markup:

老问题,但我想分享我的首选方法,它的优点是没有在您的标记中嵌入讨厌的 javascript:

CSS

CSS

a {
  color: inherit;
  text-decoration: none;
}

HTML

HTML

<a href="http://example.com" target="_blank"><input type="button" value="Link-button"></a>

回答by Siddharth Shukla

Open in new tab using javascript

使用 javascript 在新标签页中打开

 <button onclick="window.open('https://www.our-url.com')" id="myButton" 
 class="btn request-callback" >Explore More  </button>

回答by Obaidul Hye

I just used target="_blank" under form tag and it worked fine with FF and Chrome where it opens in a new tag but with IE it opens in a new window.

我只是在表单标签下使用了 target="_blank" 并且它在 FF 和 Chrome 中运行良好,它在新标签中打开,但在 IE 中它在新窗口中打开。

回答by Love Kumar

try this

尝试这个

<a id="link" href="www.gmail.com" target="_blank" >gmail</a>

回答by raj mori

Try this HTML:

试试这个 HTML:

<input type="button" value="Button_name" onclick="window.open('LINKADDRESS')"/>