Summary
Closes the current window, or the window on which it was called.
Syntax
window.close();
Description
When this method is called, the referenced window
is closed.
This method is only allowed to be called for windows that were opened by a script using the window.open()
method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script.
Examples
Closing a window opened with window.open()
This example demonstrates how to use this method to close a window opened by script calling window.open()
.
<script type="text/javascript"> //Global var to store a reference to the opened window var openedWindow; function openWindow() { openedWindow = window.open('moreinfo.htm'); } function closeOpenedWindow() { openedWindow.close(); } </script>
Closing the current window
When you call the window
object's close()
method directly, rather than calling close()
on a window
instance, the browser will close the frontmost window, whether your script created that window or not. (Firefox 35.0.1: scripts can not close windows, they had not opened)
<script type="text/javascript"> function closeCurrentWindow() { window.close(); } </script>
Specification
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of 'window.close()' in that specification. |
Living Standard |