title: Capturing result of window.onbeforeunload confirmation dialog
body: <p>Is there a way to capture to result of the <code>window.onbeforeunload</code> confirmation dialog like the one below from Stack Overflow (this happens when leaving the 'Ask Question' page without posting the question)?</p>
  <p>This is how it appears in Chrome, I believe it's slightly different in other browsers, but you always have some form of yes/no buttons.</p>
  <p><img src="https://i.stack.imgur.com/voeTJ.png" alt="window.onbeforeunload confirmation dialog" /></p>
  <p>Presumably if they're still on the offending page after the event has been triggered they chose to stay and you could probably figure this out by watching the sequence of js. However <strong>I would like to know how to determine if they clicked &quot;Leave this page&quot;</strong>?</p>
  <p>I've implemented this like below:</p>
  <pre><code>// concept taken from SO implementation
  function setConfirmUnload(showMessage, message) {
      window.onbeforeunload = showMessage ? (message ? message : &quot;this is a default message&quot;) : null;
  }
  
  // pseudo code
  listen to changes on inputs
  if any inputs fire a change event
     call setConfirmUnload(true, 'My warning message')
  </code></pre>
  <p><strong>note</strong> I'm using jQuery within my site.</p>
  <p>I'm essentially trying to implement a Gmail like drafting implementation, wherein if a user leaves a page with a form they've made changes to without saving they're warmed with a similar dialog. If they choose to discard they're changes and leave the page, I need to clean up some temporary records from the database (I'm thinking an AJAX call, or simply submitting the form with a <code>delete</code> flag) then sending them on their way.</p>
  <p>My question also relates to:</p>
  <p><a href="https://stackoverflow.com/questions/5034216/jquery-ajax-call-in-onunload-handler-firing-after-getting-the-page-on-a-manual-re">jQuery AJAX call in onunload handler firing AFTER getting the page on a manual refresh. How do I guarantee onunload happens first?</a></p>