Close a Browser Tab/Window with JavaScript

Web Dev
Sponsored links

In this article, I will introduce how to close a browser tab/window with JavaScript.

The Window.close() method

The Window.close() method is a function that closes the browser tab.

The syntax is below.


This method is only available for browser windows opened using the method.
If you use the Window.close() method on the window that has not been opened by the method, the following error message will be displayed on the console:
Scripts may not close windows that were not opened by script.

Sponsored links

Close a browser window

Below is sample code that opens a new window and closes it after 3 seconds.

let _window;

// open a browser window
_window ='');

// close the window
setTimeout(() => _window.close(), 3000)