How to close current tab in a browser window using JavaScript? Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will see how to close the current tab in a browser window using JavaScript. window.close() methodTo make this, we will use window.close() method. This method is used to close the window which is opened by the window.open() method. Syntax:window.close();But accounting for a security feature, that 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 the GeeksforGeeks window and then close it. html <!DOCTYPE html> <html lang="en"> <head> <title> How to close current tab in a browser window using JavaScript? </title> </head> <body style="text-align: center;"> <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> let myGeeksforGeeksWindow; function openWin() { myGeeksforGeeksWindow = window .open("https://www.geeksforgeeks.org/", "_blank", "width=786, height=786"); } function closeWin() { myGeeksforGeeksWindow.close(); } </script> </body> </html> Output: Output Comment More infoAdvertise with us Next Article How To Change The Browser To Fullscreen with JavaScript? S SohomPramanick Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to detect browser or tab closing in JavaScript ? Detecting browser or tab closure in JavaScript is essential for preventing data loss or unintended navigation. Using the beforeunload event, developers can prompt users with a confirmation dialog, ensuring they don't accidentally leave a page with unsaved changes or important information. The before 2 min read How To Change The Browser To Fullscreen with JavaScript? In web development, there may be scenarios where you want to display your web application or a specific element in full-screen mode, covering the entire browser window. This can be useful for creating immersive experiences, such as games, presentations, or video players. JavaScript provides a built- 2 min read How to open URL in a new window using JavaScript? In HTML, the anchor tag (<a>) is used to open new windows and tabs in a very straightforward manner. However, there are situations where you need to achieve the same functionality using JavaScript. This is where the window.open() method becomes useful.The window.open() method is used to open a 3 min read How to close window using JavaScript which is opened by the user with a URL ? JavaScript does not allow one to close a window opened by the user, using the window.close() method due to security issues. However, we can close a window by using a workaround. The approach to be followed is by opening the current URL using JavaScript so that it could be closed with a script.Syntax 2 min read How to Create Tab Headers using CSS & JavaScript? Tab headers are a commonly used user interface element in web development. They provide a way to organize content into distinct sections, allowing users to switch between them easily. Each tab represents a different category or topic, and clicking on a tab displays the corresponding content while hi 2 min read How to Create Full-Page Tabs using CSS & JavaScript? Tabs are a common UI pattern used to organize content into multiple sections within a single page. In this article, we'll explore how to create full-page tabs using CSS and JavaScript. Full-page tabs allow users to switch between different sections of content while maintaining a clean and intuitive 3 min read Like