Open In App

How to Open URL in New Tab using JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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

  • To open a new tab, we have to use _blank in the second parameter of the window.open() method.
  • The return value of window.open() is a reference to the newly created window or tab or null if it failed.
  • Do not add a third parameter to it as it will result in the opening of a new window rather than a tab

Syntax:

window.open(URL, '_blank');

Example 1:

HTML
<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
<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.


Last Updated : 15 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads