166

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

0

16 Answers 16

211

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;"
Sign up to request clarification or add additional context in comments.

2 Comments

@wong2 Might want to change answer to this one. The one currently selected is far to wordy to be efficient.
In many case, especially if you develop in local, it is better to specify relative url, so window.open('/page')
129

In javascript you can do:

window.open(url, "_blank");

Comments

48

Adding target="_blank" should do it:

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

5 Comments

I see, sorry I've missed that. Well, you could style the link to look and feel like a button. If you're using jQuery UI just use button to do so.
Excellent solution for the vast majority of us who merely dabble in web design! Thank you!
+1 because you gave a good solution to those looking for a simple "open link in new tab" answer, if they aren't using a button.
+1, if something opens a page in new tab, it is logically a link, so <a> tag is appropriate. Styles should be used to make it look like a button.
There are a lot of VALID reasons on why you have to use a <button> for link and can't replace it with <a>. e.g. you're using a HTML template and the author styled social icons based on <button> element like button.social-icon{ width:... }. It's not a good idea to edit the author styles because the 'update' process will become a pain in your somewhere...
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.

1 Comment

Works as required (tested on chrome, firefox, IE10) :)
17

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>

2 Comments

Well, I guess I have a new preferred method.
Prefer the a input[type=button] CSS selector instead of just a
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.

Comments

8

Open in new tab using javascript

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

Comments

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.

1 Comment

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

For anyone that is reading this post in 2024, you can try this:

    // Set the contract button to open the contract in a new tab
    mybutton.addEventListener('click', (event) => {
        event.preventDefault(); //to avoid changes the current page url
        window.open(myurl, '_blank');//the behavior is defined by the browser and user options
        return false;//It prevents the default action associated with the event
    });

Comments

1

try this

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

Comments

1

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>

1 Comment

You can also replace the button with a div or any other tag.
0

Use button as anchor tag

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

Comments

-2

Try this HTML:

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

Comments

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

2 Comments

you need to add "form"
It's not a good idea (semantically speaking) to have a button to behave like a link. Instead, use the a tag.
-7

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

Comments

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

2 Comments

This is not the best answer! Solid -1.
USe this code is not really an answer and you have no motivation behind it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.