How to Open URL in New Tab using JavaScript

To open the URL in New Tab using JavaScript, you can use the window.open()” method. It is used to open a new browser window or tab depending on the browser setting and the parameter values.

Syntax

window.open(URL, '_blank');

Step-by-step guide

  1. Use the window.open() method. This method is used to open a new window or a new tab.
  2. Pass the URL you want to open as the first parameter to window.open().
  3. To ensure the URL opens in a new tab, pass ‘_blank’ as the second parameter to window.open(). This tells the browser to open the URL in a new tab or window.
  4. The window.open() method returns a Window object representing the newly opened window or tab. You can use this Window object to control and interact with the new window or tab. If the new window or tab could not be opened, the window.open() will return null.

Example 1

<!DOCTYPE html>
<html>
<head>
 <title>Open URL in New Tab</title>
</head>
<body>
  <button onclick="openNewTab()">Open askjavascript.com in new tab</button>

  <script>
    function openNewTab() {
      window.open('https://askjavascript.com', '_blank');
    }
 </script>
</body>
</html>

Output

Open URL in New Tab using JavaScript

If you click the link, you will see that the new tab will be opened with the URL: https://askjavascript.com

Example 2

<!DOCTYPE html>
<html>

<head>
 <title>Open URL in New Tab</title>
</head>

<body>
 <button onclick="openNewTab()">Open OpenAI in new tab</button>

 <script>
  function openNewTab() {
    window.open('https://openai.com', '_blank');
  }
 </script>
</body>

</html>

Output

Example 2

That’s it.