Window: close() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The Window.close() method closes the current window, or the window on which it was called.

This method can only be called on windows that were opened by a script using the Window.open() method, or on top-level windows that have a single history entry. If the window doesn't match these requirements, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script.

Note also that close() has no effect when called on Window objects returned by HTMLIFrameElement.contentWindow.

Syntax

js
close()

Parameters

None.

Return value

None (undefined).

Examples

Closing a window opened with window.open()

This example shows a method which opens a window and a second one which closes the window; this demonstrates how to use Window.close() to close a window opened by calling window.open().

js
//Global variable to store a reference to the opened window
let openedWindow;

function openWindow() {
  openedWindow = window.open("more-info.htm");
}

function closeOpenedWindow() {
  openedWindow.close();
}

Specifications

Specification
HTML Standard
# dom-window-close-dev

Browser compatibility

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.