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

问题描述:

我希望有一个按钮可以重定向到给定的URL并在新选项卡中打开。如何才能做到这一点?Javascript - 通过单击按钮在新选项卡中打开给定的URL

+3

它的时间来接受卢克奥尔德顿的答案... – dit 2015-10-24 20:21:35

使用此:

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

为我工作,它就会打开一个实际的新的“弹出式”窗口,而不是一个新的完整的浏览器或标签。您也可以变量添加到它从出特定的浏览器特性停止如下:

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

@ wong2可能要更改此答案。目前选择的那个人很罗嗦,要高效。 – CSS 2015-09-25 23:02:37

你不能。此行为仅适用于插件,只能由用户配置。

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

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

添加目标=“_空白”应该这样做:

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

我用一个按钮! – wong2 2011-06-10 08:46:42

+3

我明白了,对不起,我错过了。那么,你可以设计链接的外观和感觉像一个按钮。如果您使用jQuery UI,只需使用[button](http://jqueryui.com/demos/button/)即可。 – 2011-06-10 09:12:04

+1

我们绝大多数人只是涉足网页设计的绝佳解决方案!谢谢! – 2013-04-04 05:57:55

<BUTTON NAME='my_button' VALUE=sequence_no TYPE='SUBMIT' style="background-color:transparent ; border:none; color:blue;" onclick="this.form.target='_blank';return true;"><u>open new page</u></BUTTON> 

此按钮会看起来像一个URL,并且可以在新标签中打开。

+0

你需要添加“表单” – nerkn 2014-06-27 14:29:27

+0

这不是一个好主意(从语义上讲)有一个按钮的行为像一个链接。相反,使用'a'标签。 – 2016-09-19 18:49:41

使用此代码

function openBackWindow(url,popName){ 
     var popupWindow = window.open(url,popName,'scrollbars=1,height=650,width=1050'); 
      if($.browser.msie){ 
      popupWindow.blur(); 
      window.focus(); 
     }else{ 
      blurPopunder(); 
     } 
     }; 

    function blurPopunder() { 
      var winBlankPopup = window.open("about:blank"); 
      if (winBlankPopup) { 
       winBlankPopup.focus(); 
       winBlankPopup.close() 
      } 
    }; 

它工作在Mozilla,IE罚款和铬并小于22版;但在Opera和Safari中不起作用。

+2

这不是最好的答案!固体-1。 – 2014-12-05 07:21:51

+0

使用此代码并不是真正的答案,您也没有动机。 – Edeph 2015-04-14 09:18:27

我刚刚在表单标签下使用了target =“_ blank”,它在FF和Chrome中运行良好,它在新标签中打开,但在IE中打开新窗口。

+1

选项卡或窗口是否打开取决于用户的浏览器选项。我不知道IE,但是在大多数浏览器中,网页无法选择链接在标签页还是窗口中打开,但用户可以配置它。 – joeytwiddle 2014-02-24 07:12:43

您可以忘记使用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> 

这将在一个新的标签,无论哪个浏览器客户端使用,由于target="_blank"属性的常开。

如果您只需要重定向而不使用动态参数,则可以使用TimBüthe所建议的target="_blank"属性的链接。

+1

按需要工作(在Chrome,Firefox,IE10上测试):) – Zeeshan 2013-09-14 04:44:29

试试这个HTML:

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

在javascript中,你可以这样做:

window.open(url, "_blank"); 

老问题,但我想分享我的首选方法,它有没有讨厌的JavaScript的优势被嵌入在您的标记:

CSS

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

HTML

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

嗯,我想我有一个新的首选方法。 – Halter 2017-05-16 20:00:31

+0

我认为这个更正确地回答了这个问题。 +1 :) – 2017-07-20 16:51:16