Wednesday, December 25, 2024
Google search engine
HomeLanguagesJavascriptHow to Open URL in New Tab using JavaScript ?

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 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 Geeksforneveropen
    </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()">Geeksforneveropen</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:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

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.

RELATED ARTICLES

Most Popular

Recent Comments