Window: close() method
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheWindow.close()
method closes the current window, orthe window on which it was called.
This method can only be called on windows that were opened by a script using theWindow.open()
method, or on top-level windows that have a single history entry. If the window doesn't match these requirements, an errorsimilar to this one appears in the console:Scripts may not close windows that were not opened by script.
Note also thatclose()
has no effect when called onWindow
objects returned byHTMLIFrameElement.contentWindow
.
Syntax
close()
Parameters
None.
Return value
None (undefined
).
Examples
Closing a window opened withwindow.open()
This example shows a method which opens a window and a second one which closes thewindow; this demonstrates how to useWindow.close()
to close a windowopened by callingwindow.open()
.
// Global variable to store a reference to the opened windowlet openedWindow;function openWindow() { openedWindow = window.open("more-info.htm");}function closeOpenedWindow() { openedWindow.close();}
Specifications
Specification |
---|
HTML # dom-window-close-dev |