title: How to show the "Are you sure you want to navigate away from this page?" when changes committed?
body: <p>Here in stackoverflow, if you started to make changes then you attempt to navigate away from the page, a javascript confirm button shows up and asks: "Are you sure you want to navigate away from this page?" blee blah bloo...</p>
  
  <p>Has anyone implemented this before, how do I track that changes were committed?
  I believe I could do this myself, I am trying to learn the good practices from you the experts.</p>
  
  <p>I tried the following but still doesn't work:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
      &lt;p&gt;Close the page to trigger the onunload event.&lt;/p&gt;
      &lt;script type="text/javascript"&gt;
          var changes = false;        
          window.onbeforeunload = function() {
              if (changes)
              {
                  var message = "Are you sure you want to navigate away from this page?\n\nYou have started writing or editing a post.\n\nPress OK to continue or Cancel to stay on the current page.";
                  if (confirm(message)) return true;
                  else return false;
              }
          }
      &lt;/script&gt;
  
      &lt;input type='text' onchange='changes=true;'&gt; &lt;/input&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Can anyone post an example?</p>