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. More about this tag can be learnt from this article. However, sometimes there’s a need to do the same using Javascript. In this case window.open() method proves to be helpful. 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 a new tab, we have to use _blank in second parameter of window.open().
  • 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<body>
  
    <p>Click the button to open a new tab </p>
  
    <button onclick="NewTab()">
      Open Geeksforgeeks
  </button>
  
    <script>
        function NewTab() {
            window.open(
              "https://www.geeksforgeeks.org", "_blank");
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:



Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<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>

chevron_right


Output :

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.