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. If the window was not opened by a script, 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
window.close();
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()
.
//Global var to store a reference to the opened window
var openedWindow;
function openWindow() {
openedWindow = window.open('moreinfo.htm');
}
function closeOpenedWindow() {
openedWindow.close();
}
Closing the current window
In the past, when you called the window
object's close()
method directly, rather than calling close()
on a window
instance, the browser closed the frontmost window, whether your script created that window or not. This is no longer the case; for security reasons, scripts are no longer allowed to close windows they didn't open. (Firefox 46.0.1: scripts can not close windows, they had not opened)
function closeCurrentWindow() {
window.close();
}
Specification
Specification | Status | Comment |
HTML Living StandardThe definition of 'window.close()' in that specification. | Living Standard | |
HTML5The definition of 'Window.close()' in that specification. | Recommendation |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
close
|
Chrome
Full support 1 |
Edge
Full support 12 |
Firefox Full support 1 Full support 1 Notes' Starting in Firefox 46.0.1, |
IE
Full support 4 |
Opera
Full support 3 |
Safari
Full support 1 |
WebView Android
Full support 1 |
Chrome Android
Full support 18 |
Firefox Android Full support 4 Full support 4 Notes' Starting in Firefox 46.0.1, |
Opera Android
Full support 10.1 |
Safari iOS
Full support 1 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
- See implementation notes.'
- See implementation notes.
Window.close() by Mozilla Contributors is licensed under CC-BY-SA 2.5.