Make Button Open a Link in a New Tab in JavaScript
In this tutorial, we will learn how to create a button that opens a link in a new tab with an onclick
event.
Here is the full HTML and JavaScript code:
<button id="btn" type="button">Open Link</button>
document.getElementById('btn').addEventListener('click', function() {
window.open('https://skillsugar.com');
});
In the example above, a button with an id
of btn
has a click Event Listener assigned to it and when the user clicks the button the JavaScript window.open()
function is called, opening the link provided in a new tab.