Window.close

  • Revision slug: DOM/window.close
  • Revision title: window.close
  • Revision id: 30268
  • Created:
  • Creator: Jabez
  • Is current revision? No
  • Comment /* Closing a window opened with window.open() */

Revision Content

{{template.DomRef()}}

Summary

Closes the current window, or a referenced window.

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()

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

Specification

DOM Level 0. window.close() is not part of any W3C specification or technical recommendation.

Additional Reference

MSDN window.close()

{{ wiki.languages( { "fr": "fr/DOM/window.close" } ) }}

Revision Source

<p>
{{template.DomRef()}}
</p>
<h3 name="Summary"> Summary </h3>
<p>Closes the current window, or a referenced window.
</p>
<h3 name="Syntax"> Syntax </h3>
<pre>window.close();
</pre>
<h3 name="Description"> Description </h3>
<p>When this method is called, the referenced window is closed. 
</p><p>This method is only allowed to be called for windows that were opened by a script using the <a href="en/Window.open">window.open</a> method. If the window was not opened by a script, the following error appears in the JavaScript Console: <code>Scripts may not close windows that were not opened by script.</code>
</p>
<h4 name="Examples"> Examples </h4>
<p>Closing a window opened with window.open()
</p>
<pre>&lt;script type="text/javascript"&gt;
//Global var to store a reference to the opened window
var openedWindow;

function openWindow()
{
  openedWindow = window.open('moreinfo.htm');
}
function closeOpenedWindow()
{
  openedWindow.close();
}
&lt;/script&gt;
</pre>
<h5 name="Closing_the_current_window"> Closing the current window </h5>
<pre>&lt;script type="text/javascript"&gt;
function closeCurrentWindow()
{
  window.close();
}
&lt;/script&gt;
</pre>
<h3 name="Specification"> Specification </h3>
<p>DOM Level 0. <code>window.close()</code> is not part of any W3C specification or technical recommendation.
</p>
<h3 name="Additional_Reference"> Additional Reference </h3>
<p><a class="external" href="http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/close_0.asp">MSDN window.close()</a>
</p>{{ wiki.languages( { "fr": "fr/DOM/window.close" } ) }}
Revert to this revision