title: how to use window.onbeforeunload?
body: <p>I have a small webpage. i need to activate my ajax function when browser is closed</p>
  
  <p>I using below JavaScript for that in the <code>&lt;head&gt;</code></p>
  
  <pre><code>&lt;script language="JavaScript" type="text/javascript"&gt;
   window.onbeforeunload = confirmExit;
    function confirmExit()
    {    
      my ajax_function();
      return "";
    }
  &lt;/script&gt;
  </code></pre>
  
  <p>But this function works all in the <strong>Back button, Forward button, ALT+f5, CTL+f5, Submit etc...</strong> But i need this function only when browser is closed</p>
  
  <p>But i try like below in my webpage to block the events like <strong>Back button, Forward button, ALT+f5, CTL+f5, Submit</strong> etc...</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(function () { 
  
  $('a').click(function(){window.onbeforeunload = null;});
  $(window).keydown(function(event) { 
    if (event.keyCode == 116) { // User presses F5 to refresh
       window.onbeforeunload = null;
     }});
  $('form').submit(function() {
  
     window.onbeforeunload = null;
  });
  
  });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>But this only for hyperlink, refresh, and submit. </p>
  
  <p>What i need is, <code>window.onbeforeunload</code> function only works <code>when browser is closed</code></p>