154

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

wong2
  • 34,358
  • 48
  • 134
  • 179

15 Answers15

204

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;"
Luke Alderton
  • 3,198
  • 1
  • 23
  • 34
  • 3
    @wong2 Might want to change answer to this one. The one currently selected is far to wordy to be efficient. – CSS Sep 25 '15 at 23:02
116

In javascript you can do:

window.open(url, "_blank");
satnam
  • 10,719
  • 5
  • 32
  • 42
45

Adding target="_blank" should do it:

<a id="myLink" href="www.google.com" target="_blank">google</a>
Tim Büthe
  • 62,884
  • 17
  • 92
  • 129
27

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:

<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.

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.

Tony
  • 364
  • 4
  • 3
16

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>
swinn
  • 869
  • 1
  • 16
  • 46
12

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.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
7

Open in new tab using javascript

 <button onclick="window.open('https://www.our-url.com')" id="myButton" 
 class="btn request-callback" >Explore More  </button>
Siddharth Shukla
  • 1,063
  • 15
  • 14
2

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.

Obaidul Hye
  • 119
  • 2
  • 6
  • 1
    Whether a tab or window opens is determined by the user's browser options. I don't know about IE, but in most browsers the webpage cannot choose whether the link opens in a tab or a window, but the user can configure it. – joeytwiddle Feb 24 '14 at 07:12
1

try this

<a id="link" href="www.gmail.com" target="_blank" >gmail</a>
Love Kumar
  • 1,056
  • 9
  • 10
0

In React or Javascript we can do:

<button onClick={() => window.open(url, "_blank")}> content </button>

To use an Anchor tag:

<a href="url" target="_blank" > content </a>
Aun Shahbaz Awan
  • 559
  • 7
  • 10
-1

Use button as anchor tag

<button
     target="_blank"
     rel="noreferrer"
     as="a"
     href="https://example.com"
   >
   Open
   </button>
-2

Try this HTML:

<input type="button" value="Button_name" onclick="window.open('LINKADDRESS')"/>
Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
raj mori
  • 13
  • 1
-6
<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>

This button will look like a URL and can be opened in a new tab.

Jolene
  • 1
-7

USE this code

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

IT works fine in Mozilla,IE and chrome on and less than 22 version; but doesn't work in Opera and Safari.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
-7

You can't. This behaviour is only available for plugins and can only be configured by the user.

ckruse
  • 9,642
  • 1
  • 25
  • 25