Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1110717 of beforeunload

  • Revision slug: Web/Events/beforeunload
  • Revision title: beforeunload
  • Revision id: 1110717
  • Created:
  • Creator: gwokae
  • Is current revision? No
  • Comment

Revision Content

{{EventRef}}

The beforeunload event is fired when the window, the document and its resources are about to be unloaded.

When a string is assigned to the returnValue Event property, a dialog box appears, asking the users for confirmation to leave the page (see example below). When no value is provided, the event is processed silently.

Bubbles No
Cancelable Yes
Target objects defaultView
Interface {{domxref("Event")}}

Properties

Property Type Description
target {{readOnlyInline}} {{domxref("EventTarget")}} The event target (the topmost target in the DOM tree).
type {{readOnlyInline}} {{domxref("DOMString")}} The type of event.
bubbles {{readOnlyInline}} {{jsxref("Boolean")}} Does the event normally bubble?
cancelable {{readOnlyInline}} {{jsxref("Boolean")}} Is it possible to cancel the event?
returnValue {{domxref("DOMString")}} The current return value of the event (the message to show the user).

Examples

window.addEventListener("beforeunload", function (event) {
  event.returnValue = "\o/";
});

// is equivalent to
window.addEventListener("beforeunload", function (event) {
  event.preventDefault();
});

WebKit-based browsers don't follow the spec for the dialog box. An almost cross-browser working example would be close to the following:

window.addEventListener("beforeunload", function (e) {
  var confirmationMessage = "\o/";

  e.returnValue = confirmationMessage;     // Gecko, Trident, Chrome 34+
  return confirmationMessage;              // Gecko, WebKit, Chrome <34
});

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. In Firefox 4 and later the returned string is not displayed to the user. Instead, Firefox displays the string "This page is asking you to confirm that you want to leave - data you have entered may not be saved." See {{bug("588292")}}.

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

Note also that various mobile browsers ignore the result of the event (that is, they do not ask the user for confirmation). Firefox has a hidden preference in about:config to do the same. In essence this means the user always confirms that the document may be unloaded.

See also

Revision Source

<p>{{EventRef}}</p>

<p>The <strong><code>beforeunload</code></strong> event is fired when the window, the document and its resources are about to be unloaded.</p>

<p>When a string is assigned to the <code>returnValue</code> Event property, a dialog box appears, asking the users for confirmation to leave the page (see example below). When no value is provided, the event is processed silently.</p>

<table class="properties">
 <tbody>
  <tr>
   <td>Bubbles</td>
   <td>No</td>
  </tr>
  <tr>
   <td>Cancelable</td>
   <td>Yes</td>
  </tr>
  <tr>
   <td>Target objects</td>
   <td>defaultView</td>
  </tr>
  <tr>
   <td>Interface</td>
   <td>{{domxref("Event")}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Properties">Properties</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Property</th>
   <th scope="col">Type</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>target</code> {{readOnlyInline}}</td>
   <td>{{domxref("EventTarget")}}</td>
   <td>The event target (the topmost target in the DOM tree).</td>
  </tr>
  <tr>
   <td><code>type</code> {{readOnlyInline}}</td>
   <td>{{domxref("DOMString")}}</td>
   <td>The type of event.</td>
  </tr>
  <tr>
   <td><code>bubbles</code> {{readOnlyInline}}</td>
   <td>{{jsxref("Boolean")}}</td>
   <td>Does the event normally bubble?</td>
  </tr>
  <tr>
   <td><code>cancelable</code> {{readOnlyInline}}</td>
   <td>{{jsxref("Boolean")}}</td>
   <td>Is it possible to cancel the event?</td>
  </tr>
  <tr>
   <td><code>returnValue</code></td>
   <td>{{domxref("DOMString")}}</td>
   <td>The current return value of the event (the message to show the user).</td>
  </tr>
 </tbody>
</table>

<h2 id="Examples">Examples</h2>

<pre class="brush:js;">
window.addEventListener("beforeunload", function (event) {
  event.returnValue = "\o/";
});

// is equivalent to
window.addEventListener("beforeunload", function (event) {
  event.preventDefault();
});</pre>

<p>WebKit-based browsers don't follow the spec for the dialog box. An almost cross-browser working example would be close to the following:</p>

<pre class="brush: js">
window.addEventListener("beforeunload", function (e) {
  var confirmationMessage = "\o/";

  e.returnValue = confirmationMessage;     // Gecko, Trident, Chrome 34+
  return confirmationMessage;              // Gecko, WebKit, Chrome &lt;34
});</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. In Firefox 4 and later the returned string is not displayed to the user. Instead, Firefox displays the string "This page is asking you to confirm that you want to leave - data you have entered may not be saved."&nbsp;See {{bug("588292")}}.</p>

<p>Since 25 May 2011, the HTML5 specification states that calls to {{domxref("window.alert()")}}, {{domxref("window.confirm()")}}, and {{domxref("window.prompt()")}} methods may be ignored during this event. See the <a href="https://www.w3.org/TR/html5/webappapis.html#user-prompts">HTML5 specification</a> for more details.</p>

<p>Note also that various mobile browsers ignore the result of the event (that is, they do not ask the user for confirmation). Firefox has a hidden preference in about:config to do the same. In essence this means the user always confirms that the document may be unloaded.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{Event("DOMContentLoaded")}}</li>
 <li>{{Event("readystatechange")}}</li>
 <li>{{Event("load")}}</li>
 <li>{{Event("unload")}}</li>
 <li><a href="https://www.whatwg.org/specs/web-apps/current-work/#prompt-to-unload-a-document" title="https://www.whatwg.org/specs/web-apps/current-work/#prompt-to-unload-a-document">Unloading Documents — Prompt to unload a document</a></li>
 <li><a href="/en-US/docs/">Remove Custom Messages in onbeforeload Dialogs&nbsp;after Chrome 51</a>&nbsp;</li>
</ul>
Revert to this revision