Skip to content
Related Articles

Related Articles

Improve Article
How to Open URL in New Tab using JavaScript ?
  • Difficulty Level : Basic
  • Last Updated : 27 Sep, 2019

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:




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

Output:

Example 2:




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

Output :




My Personal Notes arrow_drop_up
Recommended Articles
Page :