title: jquery beforeunload when closing (not leaving) the page?
body: <p>How can I display "Are you sure you want to leave the page?" when the user actually tries to close the page (click the X button on the browser window or tab) not when he tries to navigate away from the page (click on another link).</p>
  
  <p>My client wants a message to appear when the user tries to close the page "Are you sure you want to leave the page? You still have items in your shopping cart."</p>
  
  <p>Unfortunately <code>$(window).bind('beforeunload')</code> doesn't fire only when the user closes the page.</p>
  
  <p><strong>jQuery:</strong></p>
  
  <pre><code>function checkCart() { 
    $.ajax({
      url : 'index.php?route=module/cart/check',
      type : 'POST',
      dataType : 'json',
      success : function (result) {
         if (result) {
          $(window).bind('beforeunload', function(){
            return 'leave?';
          });
         }
      }
    })
  }
  </code></pre>