I would like to have a button that redirects to a given URL and opens in a new tab. How can this be done?
16 Answers
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;"
Adding target="_blank" should do it:
<a id="myLink" href="www.google.com" target="_blank">google</a>
5 Comments
<a> tag is appropriate. Styles should be used to make it look like a button.<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...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
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
a input[type=button] CSS selector instead of just aI 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
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
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
<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
a tag.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.