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.

Windows are script-closable if they were created by web content. This generally includes:

  • Windows opened using Window.open()
  • Windows opened via web content, such as links (<a target="_blank">) or forms (<form target="_blank">), without user modifier actions

Windows opened by browser UI actions — such as right-click → Open in new tab, Ctrl+Click, Shift+Click, or middle-click — are often not script-closable. They may only be closed if they have not been navigated (history length remains 1). Calling close() otherwise typically shows a console warning: 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
# dom-window-close-dev

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
close
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote
footnote Before Chrome 95, script-closeability depended on having an opener, not on being created by web content.
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote
footnote Before Firefox 128, script-closeability depended on having an opener, not on being created by web content.
Opera – Full support
Opera 3 (Release date: 1997-12-01)
footnote Full support
Safari – Full support
Safari 1 (Release date: 2003-06-23)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote
footnote Before Chrome Android 95, script-closeability depended on having an opener, not on being created by web content.
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote
footnote Before Firefox for Android 128, script-closeability depended on having an opener, not on being created by web content.
Opera Android – Full support
Opera Android 10.1 (Release date: 2010-11-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 1 (Release date: 2007-06-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote
footnote Before Samsung Internet 17.0, script-closeability depended on having an opener, not on being created by web content.
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote
footnote Before WebView Android 95, script-closeability depended on having an opener, not on being created by web content.
WebView on iOS – Full support
WebView on iOS 1 (Release date: 2007-06-29)
footnote Full support
Deno – Partial support
Deno 1 (Release date: 2020-05-13)
footnote Partial support
footnote Exits the current Deno process.

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
See implementation notes.