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

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.

Syntax

window.close();

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>
//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>
function closeCurrentWindow()
{
  window.close();
}
</script>

Specification

Specification Status Comment
WHATWG HTML Living Standard
The definition of 'window.close()' in that specification.
Living Standard  

Additional reference

MSDN: window.close Method

Document Tags and Contributors

 Last updated by: teoli,