Opening a new browser window in a new tab can be done easily in JavaScript.
Modern web browsers provide a simple API that creates an interface called the window object.
Since the goal is to open a new location in the current browser window, we need to utilize the window interface’s open() method.
The window.open() method takes three parameters: the first being the URL, the second being the window name, and the third being the window features.
If we pass the value of ‘_blank’ as the window name (second parameter), the browser will know to open the URL in a new tab.
Note: It may seem like the 3rd parameter would be where we would want to specify ‘_blank’ as it seems like it would be considered to be a “window feature,” but that parameter actually exists to pass “name=value” pairs to the new window. This utilization of the window.open() method is very rare is unlikely to be needed outside of websites that use frames.
Open a New Tab with JavaScript Example
This is a straightforward example that will open the URL specified as the first parameter in a new tab.
window.open('https://actualwizard.com', '_blank');
The first example was a little bit generic, so here’s an example of pressing a button to open the URL in a new tab.
<script>
function open_url () {
window.open('https://actualwizard.com', '_blank');
}
</script>
<button onclick="open_url()">Open URL in a New Tab</button>
Code Demo: