close

  • Revision slug: Web/Reference/Events/close_event
  • Revision title: close
  • Revision id: 402707
  • Created:
  • Creator: Sheppy
  • Is current revision? Yes
  • Comment Moved From DOM/Mozilla_event_reference/close_event to Web/Reference/Events/close_event

Revision Content

The close event is executed when a request has been made to close the window when the user presses the close button.

If an event handler is placed on the window element, it can be prevented to close (see example below). Note that the close event is only fired when the user presses the close button on the titlebar; (i.e. not File -> Quit). The unload event should be used to capture all attempts to unload the window.

General info

Specification
XUL
Interface
Event
Bubbles
No
Cancelable
No
Target
window
Default Action
The window is closed.

Properties

{{OpenEventProperties()}}{{CloseEventProperties()}}

Example

window.addEventListener("close", function( event ) {
  // make the close button ineffective
  event.preventDefault();
}, false);

Revision Source

<p>The <code>close</code> event is executed when a request has been made to close the window when the user presses the close button.</p>
<p>If an event handler is placed on the window element, it can be prevented to close (see example below). Note that the close event is only fired when the user presses the close button on the titlebar; (i.e. not File -&gt;&nbsp;Quit). The <a href="/en-US/docs/Mozilla_event_reference/unload" title="/en-US/docs/Mozilla_event_reference/unload">unload</a> event should be used to capture all attempts to unload the window.</p>
<h2 id="General_info">General info</h2>
<dl>
  <dt style="clear: left; float: left; text-align: right; width: 120px;">
    Specification</dt>
  <dd style="margin: 0 0 0 120px;">
    XUL</dd>
  <dt style="clear: left; float: left; text-align: right; width: 120px;">
    Interface</dt>
  <dd style="margin: 0 0 0 120px;">
    Event</dd>
  <dt style="clear: left; float: left; text-align: right; width: 120px;">
    Bubbles</dt>
  <dd style="margin: 0 0 0 120px;">
    No</dd>
  <dt style="clear: left; float: left; text-align: right; width: 120px;">
    Cancelable</dt>
  <dd style="margin: 0 0 0 120px;">
    No</dd>
  <dt style="clear: left; float: left; text-align: right; width: 120px;">
    Target</dt>
  <dd style="margin: 0 0 0 120px;">
    window</dd>
  <dt style="clear: left; float: left; text-align: right; width: 120px;">
    Default Action</dt>
  <dd style="margin: 0 0 0 120px;">
    The window is closed.</dd>
</dl>
<h2 id="Properties">Properties</h2>
<p>{{OpenEventProperties()}}{{CloseEventProperties()}}</p>
<h2 id="Example">Example</h2>
<pre class="brush:js;">
window.addEventListener("close", function( event ) {
  // make the close button ineffective
  event.preventDefault();
}, false);
</pre>
Revert to this revision