Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Update (2017)</h2> <p>Modern browsers now consider displaying a custom message to be a security hazard and it has therefore been <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#Browser_compatibility" rel="noreferrer">removed</a> from all of them. Browsers now only display generic messages. Since we no longer have to worry about setting the message, it is as simple as:</p> <pre><code>// Enable navigation prompt window.onbeforeunload = function() { return true; }; // Remove navigation prompt window.onbeforeunload = null; </code></pre> <p>Read below for legacy browser support.</p> <h2>Update (2013)</h2> <p>The orginal answer is suitable for IE6-8 and FX1-3.5 (which is what we were targeting back in 2009 when it was written), but is rather out of date now and won't work in most current browsers - I've left it below for reference.</p> <p>The <code>window.onbeforeunload</code> is not treated consistently by all browsers. It should be a function reference and not a string (as the original answer stated) but that will work in older browsers because the check for most of them appears to be whether anything is assigned to <code>onbeforeunload</code> (including a function that returns <code>null</code>).</p> <p>You set <code>window.onbeforeunload</code> to a function reference, but in older browsers you have to set the <code>returnValue</code> of the event instead of just returning a string:</p> <pre><code>var confirmOnPageExit = function (e) { // If we haven't been passed the event get the window.event e = e || window.event; var message = 'Any text will block the navigation and display a prompt'; // For IE6-8 and Firefox prior to version 4 if (e) { e.returnValue = message; } // For Chrome, Safari, IE8+ and Opera 12+ return message; }; </code></pre> <p>You can't have that <code>confirmOnPageExit</code> do the check and return null if you want the user to continue without the message. You still need to remove the event to reliably turn it on and off:</p> <pre><code>// Turn it on - assign the function that returns the string window.onbeforeunload = confirmOnPageExit; // Turn it off - remove the function entirely window.onbeforeunload = null; </code></pre> <h2>Original answer (worked in 2009)</h2> <p>To turn it on:</p> <pre><code>window.onbeforeunload = "Are you sure you want to leave?"; </code></pre> <p>To turn it off:</p> <pre><code>window.onbeforeunload = null; </code></pre> <p>Bear in mind that this isn't a normal event - you can't bind to it in the standard way.</p> <p>To check for values? That depends on your validation framework.</p> <p>In jQuery this could be something like (very basic example):</p> <pre><code>$('input').change(function() { if( $(this).val() != "" ) window.onbeforeunload = "Are you sure you want to leave?"; }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload