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

前端 未结 13 432
一生所求
一生所求 2020-12-04 13:35

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

相关标签:
13条回答
  • 2020-12-04 14:19

    My preferred method has the advantage of no JavaScript embedded in your markup:

    CSS

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

    HTML

    <a href="http://example.com" target="_blank"><input type="button" value="Link-button"></a>
    
    0 讨论(0)
  • 2020-12-04 14:21

    Use window.open instead of window.location to open a new window or tab (depending on browser settings).

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

    0 讨论(0)
  • 2020-12-04 14:25

    Open in new tab using javascript

     <button onclick="window.open('https://www.our-url.com')" id="myButton" 
     class="btn request-callback" >Explore More  </button>
    
    0 讨论(0)
  • 2020-12-04 14:28

    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.

    0 讨论(0)
  • 2020-12-04 14:28

    Try this HTML:

    <input type="button" value="Button_name" onclick="window.open('LINKADDRESS')"/>
    
    0 讨论(0)
  • 2020-12-04 14:30

    In javascript you can do:

    window.open(url, "_blank");
    
    0 讨论(0)
提交回复
热议问题