title: How to sync with server and clear session before the user leaves a page?
body: <p>Is there any way to detect when a user leaves a page, no matter if it's by closing the browser, entering a new URL in the address bar, clicking on a link that redirects to other domain, etc. ?</p>
  
  <p>The main purpose of this would be to perform some activities such as: </p>
  
  <ul>
  <li>sync with the server some data that resides in the client side</li>
  <li>clear server session</li>
  </ul>
  
  <p>I was trying with the window's unload and beforeunload events, and reading other questions like:
  <a href="https://stackoverflow.com/questions/147636/best-way-to-detect-when-user-leaves-a-web-page">Best way to detect when a user leaves a web page?</a> 
   //
  <a href="https://stackoverflow.com/questions/1209231/is-there-any-way-to-know-that-user-leaving-a-page-with-asp-net">Is there any way to know that user leaving a page with asp.net?</a> but I didn't find the answer I would expect.</p>
  
  <p>Here is a simplified js snippet to understand what I was trying:</p>
  
  <pre><code>window.onunload = function(){
      if (theConditionThatINeed){
          doThings();
          SyncWithServerAndAbandonSession(url, localObjects);
      }
      else {
          doNothing();
      }
  }
  </code></pre>
  
  <p>I don't want to display any kind of confirmation before the user leaves, so I think that the <em>onbeforeunload</em> won't help me here.</p>
  
  <p>Supposing that the answer is "there is no way to do such thing", what would be the recommended practice to accomplish the synchronization and session clearing that I want?</p>
  
  <p>The primary browser that I support is IE >= 7 </p>