How to close a current tab in a browser window using JavaScript?

We will learn to close the browser window using JavaScript in this tutorial. Suppose users have opened your website in 2 tabs and don't want to allow it, then you can close one tab automatically using JavaScript.

Also, if you have noticed that some web apps automatically open the browser window to capture a face, it closes automatically when you capture the image by clicking the button. It's all we can do using JavaScript.

This tutorial will teach different examples of closing the current tab in a browser window using JavaScript.

Note ? JavaScript never allows developers to close the window opened by the users. It is due to some security issues. So, programmers can only close the browser window opened using the open() method of JavaScript.

Syntax

You can follow the syntax below to use the close() method to close the browser's window.

let new_window = window.open(URL, width, height);
new_window.close();

In the above syntax, we have stored the returned value in the new_window variable while creating it. After that, we used the close() method to close the newly opened window.

Example: Closing a New Tab

In this example, we have two buttons; one is to open a new tab, and another is to close a new tab that is opened using JavaScript.

Users need first to open the tab using JavaScript by clicking the open new tab button. After they can click on the close new tab button, observe that it automatically closes the tab.

<html>
<body>
   <h2>Using the <i>window.close()</i> method to close the browser's tab</h2>
   <p>Click "Open New Tab" to open a page in new tab and click "Close New Tab" to close the opened tab</p>
   <button id="openTab" onclick="openTab()">Open New Tab</button><br><br>
   <button id="closeTab" onclick="closeTab()">Close New Tab</button>
   <script>
      // creating the variable to store the tab
      var new_tab;
      function openTab() {
         // open a new tab
         new_tab = window.open("/index.htm");
      }
      function closeTab() {
         // close tab
         if (new_tab) {
            new_tab.close();
         }
      }
   </script>
</body>
</html>

Example: Closing a New Window

In the example below, we have created two buttons, like the above example, to open and close the window. However, in this example, we have also added '_blank' as a parameter of the open() method, which will open a new window rather than a new tab.

Users can click the close new window button to close the pop-up window.

<html>
<body>
   <h2>Using the <i>window.close()</i> method to close the browser's window</h2>
   <p>Click "Open New Window" to open the page in new window and click "Close New Window" to close the opened window</p>
   <button id="openWindow" onclick="openWindow()">Open New Window</button><br><br>
   <button id="closeWindow" onclick="closeWindow()">Close New Window</button>
   <script>
      // creating the variable to store window
      var new_window;
      function openWindow() {
         // open a new window
         new_window = window.open("/index.htm", "_blank", "width=800,height=600");
      }
      function closeWindow() {
         // close the window
         if (new_window) {
            new_window.close();
         }
      }
   </script>
</body>
</html>

Security Considerations

Modern browsers have strict security policies regarding window closing:

  • Only windows opened by JavaScript can be closed by JavaScript
  • User-opened tabs cannot be closed programmatically
  • Some browsers may show confirmation dialogs before closing

Common Use Cases

The window.close() method is commonly used in:

  • Pop-up windows for forms or authentication
  • Photo capture applications that open camera windows
  • Payment gateway redirections
  • Modal-like experiences using separate windows

Conclusion

The window.close() method allows you to programmatically close browser windows or tabs that were opened using JavaScript. Remember that browser security policies prevent closing user-opened tabs, so this method only works on windows created with window.open().

Updated on: 2026-03-15T23:19:00+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements