Open In App

How to Open URL in New Tab using JavaScript ?

In HTML, the anchor tag is used to open URLs in a new tab in an elementary and straightforward manner. However, sometimes there’s a need to do the same using Javascript.

In JavaScript window.open() method is used to open links in a new tab. The window.open() method is used to open a new browser window or a new tab depending on the browser setting and the parameter values.

Approach to Open URL in New Tab using JavaScript

Syntax:

window.open(URL, '_blank');

Example 1:

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

    
<p> Click the button to open 
          <b> geeksforgeeks.org </b>
          in new tab 
      </p>


    <button onclick="NewTab()">
        Open Geeksforgeeks
    </button>

    <script>
        function NewTab() {
            window.open(
            "https://www.geeksforgeeks.org", "_blank");
        }
    </script>

</body>

</html>

Output:

Example 2:

<html>
  <head> 
      <title>Open Google in New Tab</title>
  </head>
  
<body>

    
<p>Click the button to open google.</p>


    <button onclick="Open()">Geeksforgeeks</button>

    <script>
        function Open() {
            window.open("https://www.google.com", "_blank");
        }
    </script>

</body>

</html>

Output:

Supported Browsers

The browsers are supported by the window.open() method are listed below:

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

HTML is the foundation of web pages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

Article Tags :