Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your probably thinking of a synchronous ajax request (which blocks the UI):</p> <pre><code>window.addEventListener('unload',function() { var xhr = new XMLHttpRequest(); xhr.open('GET', 'script.php?when=unload',false);//&lt;-- false makes request synchronous xhr.send(); },false); </code></pre> <p>But <a href="https://stackoverflow.com/questions/2970782/javascript-wait-until-ajax-request-finishes-to-close-page">there are other ways, check this answer</a><Br/> On the whole, I'd <em>not</em> do things like this. If a site attempted to deny me the option of closing the window when I feel like it, I'd never use/visit it again. That, and the fact that your JS code is still subject to how the browser implements it, and the browser can be controlled by the client's OS. If I close the browser application, a JS event has nothing to say in that matter, especially if I terminate the browser process (using <code>kill -9</code>, or ctrl+alt+del).</p> <p>The very least you can do is offer the client a choice, to either force-quit, or wait, explaining why you'd rather the client waited a while:</p> <pre><code>window.addEventListener('beforeunload',function u(e) { var forceQuit = confirm('\tDo you wish to leave Now?\n if you do, some changes you made won\'t be saved'); if (forceQuit) { return e; } //synchronous ajax result here, or: e.returnValue = false; e.cancelBubble = true; if (e.preventDefault) { e.preventDefault(); e.stopPropagation(); } setTimeout(function() {//first, remove handler, so the beforeunload's behaviour is back to default window.removeEventListener('beforeunload',u,false); //dispatch new beforeunload event: window.dispatchEvent( new Event('beforeunload')); },5000); },false); </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.
 

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