So this is the simple code for the button to open a certain link
<button class="btn btn-success" onclick="location.href='http://google.com';"> Google</button>
but it opens it on the same page, I want the link to open on a new tab though.
JavaScript is beautiful! If you want to open a link in a new tab –the easiest way is to use target attribute of ANCHOR element in HTML. Here is how you need to do it. Tip 1: Here the target attribute is set to _blank. This value opens a new tab/window.
For now, the only way to have in-app links / buttons open in a new tab is to create your own link / button with an HTML element. Write your own HTML link / button. It is this attribute (target="_blank") that causes the link to open in a new tab.
To open a new tab with a button you just have to choose any of these options: Use '_blank'. It will not only open the link in a new tab but the state of the original webpage will also remain unaffected.
There are several ways to create an HTML button, that acts like a link (i.e., clicking on it the user is redirected to the specified URL). You can choose one of the following methods to add a link to an HTML button. 1. Add inline onclick event. to HTML <button> tag within HTML <form> element. It might not work if the button is inside a <form> tag.
You can use the following.
window.open(
'https://google.com',
'_blank' // <- This is what makes it open in a new window.
);
in HTML
<button class="btn btn-success" onclick=" window.open('http://google.com','_blank')"> Google</button>
plunkr
With Bootstrap you can use an anchor like a button.
<a class="btn btn-success" href="https://www.google.com" target="_blank">Google</a>
And use target="_blank"
to open the link in a new tab.
You can also add this to your form:
<form action="https://www.google.com" target="_blank">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With