window.onbeforeunload

  • Revision slug: Web/API/window.onbeforeunload
  • Revision title: window.onbeforeunload
  • Revision id: 439041
  • Created:
  • Creator: Delapouite
  • Is current revision? No
  • Comment fixed EventTarget.addEventListener link

Revision Content

{{DomRef}}

Summary

An event that fires when a window is about to {{domxref("window.onunload","unload")}} its resources. The document is still visible and the {{domxref("event.preventDefault","event is still cancelable")}}.

Syntax

window.onbeforeunload = funcRef
  • funcRef is a reference to a function or a function expression
  • The function should assign a string value to the returnValue property of the Event object and return the same string
  • Note that in Firefox 4 and later the returned string is not displayed to the user. See {{bug("588292")}}.

Example

window.onbeforeunload = function(e) {
  return 'Dialog text here.';
};

Notes

When this event returns a non-void value, the user is prompted to confirm the page unload. In most browsers, the return value of the event is displayed in this dialog.

Since 25 May 2011, the HTML5 specification states that calls to {{domxref("window.showModalDialog()")}}, {{domxref("window.alert()")}}, {{domxref("window.confirm()")}} and {{domxref("window.prompt()")}} methods may be ignored during this event.

You can and should handle this event through {{domxref("EventTarget.addEventListener","window.addEventListener()")}} and the beforeunload event. More documentation is available there.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1 1 4 12 3
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

Specification

The event was originally introduced by Microsoft in Internet Explorer 4 and standardized in the HTML5 specification.

  • {{spec("http://dev.w3.org/html5/spec-LC/history.html#unloading-documents", "HTML5: Browsing the Web, Unloading documents", "LC")}}

See also

Revision Source

<div>
  {{DomRef}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>An event that fires when a window is about to {{domxref("window.onunload","unload")}} its resources. The document is still visible and the {{domxref("event.preventDefault","event is still cancelable")}}.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
window.onbeforeunload = <var>funcRef</var></pre>
<ul>
  <li><code>funcRef</code> is a reference to a function or a function expression</li>
  <li>The function should assign a string value to the <code>returnValue</code> property of the Event object and return the same string</li>
  <li>Note that in Firefox 4 and later the returned string is not displayed to the user. See {{bug("588292")}}.</li>
</ul>
<h2 id="Example" name="Example">Example</h2>
<pre class="brush:js">
window.onbeforeunload = function(e) {
  return 'Dialog text here.';
};
</pre>
<h2 id="Notes">Notes</h2>
<p>When this event returns a non-void value, the user is prompted to confirm the page unload. In most browsers, the return value of the event is displayed in this dialog.</p>
<p>Since 25 May 2011, the <a href="http://www.w3.org/TR/html5-diff/#changes-2011-05-25" title="http://www.w3.org/TR/html5-diff/#changes-2011-05-25">HTML5 specification</a> states that calls to {{domxref("window.showModalDialog()")}}, {{domxref("window.alert()")}}, {{domxref("window.confirm()")}} and {{domxref("window.prompt()")}} methods may be ignored during this event.</p>
<p>You <em>can</em> and <em>should</em> handle this event through {{domxref("EventTarget.addEventListener","window.addEventListener()")}} and the <a href="/en-US/docs/Mozilla_event_reference/beforeunload" title="Mozilla_event_reference/unload"><code>beforeunload</code> event</a>. More documentation is available there.</p>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>
  {{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari (WebKit)</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>1</td>
        <td>1</td>
        <td>4</td>
        <td>12</td>
        <td>3</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="Specification" name="Specification">Specification</h2>
<p>The event was originally introduced by Microsoft in Internet Explorer 4 and standardized in the HTML5 specification.</p>
<ul>
  <li>{{spec("http://dev.w3.org/html5/spec-LC/history.html#unloading-documents", "HTML5: Browsing the Web, Unloading documents", "LC")}}</li>
</ul>
<h2 id="See also" name="See also">See also</h2>
<ul>
  <li><a href="http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx">MSDN: onbeforeunload event</a></li>
</ul>
Revert to this revision