Window.close

  • Revision slug: Web/API/window.close
  • Revision title: window.close
  • Revision id: 406697
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment 16 words addedMoved From DOM/window.close to Web/API/window.close

Revision Content

{{ 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

{{ DOM0() }}

Additional Reference

{{ MSDN("ms536367", "window.close Method") }}

{{ languages( { "fr": "fr/DOM/window.close", "ja": "ja/DOM/window.close", "zh-cn": "zh-cn/DOM/window.close" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<h3 id="Summary" name="Summary">Summary</h3>
<p>Closes the current window, or a referenced window.</p>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<pre class="eval"><em>window</em>.close();
</pre>
<h3 id="Description" 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/DOM/window.open" title="en/DOM/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>
<h3 id="Examples" name="Examples">Examples</h3>
<h4 id="Closing_a_window_opened_with_window.open.28.29" name="Closing_a_window_opened_with_window.open.28.29">Closing a window opened with <code>window.open()</code></h4>
<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>
<h4 id="Closing_the_current_window" name="Closing_the_current_window">Closing the current window</h4>
<pre>&lt;script type="text/javascript"&gt;
function closeCurrentWindow()
{
  window.close();
}
&lt;/script&gt;
</pre>
<h3 id="Specification" name="Specification">Specification</h3>
<p>{{ DOM0() }}</p>
<h3 id="Additional_Reference" name="Additional_Reference">Additional Reference</h3>
<p>{{ MSDN("ms536367", "window.close Method") }}</p>

<p>{{ languages( { "fr": "fr/DOM/window.close", "ja": "ja/DOM/window.close", "zh-cn": "zh-cn/DOM/window.close" } ) }}</p>
Revert to this revision