title: ASP.NET, jQuery, dirty forms, and window.onbeforeunload
body: <p>In my ASP.NET web app, I'm trying to create a universal way of warning users before navigating away from a form when they've made changes, using jQuery. Pretty standard stuff, but after a lot of searching I have yet to find a technique that works. </p>
  
  <p>Here's what I have at this point:</p>
  
  <pre><code>    addToPostBack = function(func) {
          var old__doPostBack = __doPostBack;
          if (typeof __doPostBack != 'function') {
              __doPostBack = func;
          } else {
              __doPostBack = function() {
                  old__doPostBack();
                  func();
              }
          }
      }
  
      var isDirty = false;
  
      $(document).ready(function() {
          addToPostBack(function() {
              alert("Postback detected.")
              clearDirty();
          });
          $(':input').bind("change select keydown", setDirty);
          window.onbeforeunload = function(e) {
              var msg = "You have unsaved changes. "
              if (isDirty == true) {
                  var e = e || window.event;
                  if (e) { e.returnValue = msg; }
                  return msg;
              }
          };
      });
  
      setDirty = function() {isDirty = true;}
  
      clearDirty = function() {isDirty = false;}
  </code></pre>
  
  <p>This works as far as warning the user from navigating away. The problem is that I get the warning on every same-page postback. There are a number of things on my forms that might trigger a postback: </p>
  
  <ol>
  <li>There are Save, Cancel, and Delete linkbuttons on the page</li>
  <li>There might be other linkbuttons on the page that execute server-side functionality while staying on the same page</li>
  <li>There might be other controls with autopostback=true that also have server-side functions attached to them, but which don't result in the user leaving the page. </li>
  </ol>
  
  <p>None of these things should provoke a warning, because the user isn't leaving the page. My code tries to hijack addToPostBack (<a href="https://stackoverflow.com/questions/1230573/how-to-capture-submit-event-using-jquery-in-an-asp-net-application/1234845">more details on that in this question</a>) to clear the isDirty bit before posting back, but the problem is that in IE <code>onbeforeunload</code> fires before __doPostBack, apparently because IE fires onbeforeunload immediately when a link is clicked (<a href="https://stackoverflow.com/questions/317423/asp-net-linkbutton-raising-onbeforeunload-event-twice">as described here</a>). </p>
  
  <p>Of course, I could wire up each of these controls to clear the isDirty bit, but I'd prefer a solution that operates on the form level and that doesn't require that I touch every single control that might trigger a postback. </p>
  
  <p>Does anyone have an approach that works in ASP.NET and that doesn't involve wiring up every control that might cause a postback?</p>