We are going to be looking at the method to close the current tab in a browser window using JavaScript. Earlier it used to happen that with a simple JavaScript function, the current tab used to get closed. The following syntax was used for this purpose.
Syntax:
window.close()
But accounting for a security feature, which was introduced a few years ago, ordinary JavaScript lost its privilege to close the current tab, just by using this syntax.
Note: A current window/tab will only get closed if and only if it was created & opened by that script.Means the window.close syntax is only allowed for the window/tab that is created & opened using window.open method.
Example: This example shows how to open GeekForGeeks window and then close it.
<!DOCTYPE html> <html> <head> <style> body{ text-align:center; } </style> </head> <body> <h2 style="color:green">GeeksforGeeks</h2> <h4 style="color:purple">Close Tab/Window using JavaScript</h4> <button onclick="openWin()">Click to open GeeksforGeeks website</button> <button onclick="closeWin()">Click here to close the window</button> <script> var myGeeksforGeeksWindow; function openWin() { myGeeksforGeeksWindow = window .open("https://www.geeksforgeeks.org", "_blank", "width=786, height=786"); } function closeWin() { myGeeksforGeeksWindow.close(); } </script> </body> </html> |
Output:
When we load the code:

New Window/Tab gets opened up:

The Window/Tab gets closed:

The entire process:

Recommended Posts:
- How to remove active nav-tab when click outside of nav-tab in Bootstrap ?
- How to detect browser or tab closing in JavaScript ?
- How to get the size of screen, current web page and browser window using jQuery?
- Javascript | Window Open() & Window Close Method
- How to check a webpage is loaded inside an iframe or into the browser window using JavaScript?
- Cross-browser window resize event using JavaScript/jQuery
- How to redirect browser window back using JavaScript ?
- How to open a hyperlink in another window or tab in HTML ?
- How to close window using JavaScript which is opened by the user with a URL ?
- How to Open URL in New Tab using JavaScript ?
- How to give a div tag 100% height of the browser window using CSS
- How to print the content of current window using JavaScript ?
- Java Program to Display Current Hour and Current Minute
- How to force tab-navigation to stay in place using Bootstrap ?
- How to check which tab is active using Material UI ?
- Javascript | Window Blur() and Window Focus() Method
- How to make browser to go back to previous page using JavaScript ?
- How to stop browser back button using JavaScript ?
- How to detect the user browser ( Safari, Chrome, IE, Firefox and Opera ) using JavaScript ?
- How to Display Changed Browser URL Without Reloading Through alert 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.
Improved By : nidhi_biet


