Skip to content
Related Articles

Related Articles

Improve Article

How to Open URL in New Tab using JavaScript ?

  • Difficulty Level : Basic
  • Last Updated : 20 Jul, 2021
Geek Week

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 :

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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :