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( } </script> </body> </html> |
Output:

Example 2:
<html> <body> <p>Click the button to open google.</p> <button onclick="Open()">Geeksforgeeks</button> <script> function Open() { } </script> </body> </html> |
Output :

Recommended Posts:
- How to remove active nav-tab when click outside of nav-tab in Bootstrap ?
- How to open URL in a new window using JavaScript ?
- How to open a hyperlink in another window or tab in HTML ?
- How to close current tab in a browser window using JavaScript?
- Convert relative path URL to absolute path URL using JavaScript
- How to get the new image URL after refreshing the image using JavaScript ?
- How to detect browser or tab closing in JavaScript ?
- PHP | fopen( ) (Function open file or URL)
- How to force tab-navigation to stay in place using Bootstrap ?
- How to check which tab is active using Material UI ?
- CSS | tab-size Property
- How to tab space instead of multiple non-breaking spaces (nbsp)?
- How to Create a Tab Image Gallery ?
- Semantic-UI | Tab
- How to make bootstrap version 2 tab dropdown?
- How to set active tab style with AngularJS?
- How to assign custom URL domain Web Address in New Google Sites ?
- New self vs. new static in PHP
- Open a link without clicking on it using JavaScript
- Open all persons solution links from submission page using JavaScript
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.


