title: Kill sessions on browser/tab close
body: <p>I've been trying to solve <a href="https://stackoverflow.com/questions/1921941/close-kill-the-session-when-the-browser-or-tab-is-closed">how to close session when browser/tab is closed</a>. Following the Daniel Melo's answer + setting session's coockie TTL to 0 i'm almost ready, but Daniel's tip is <strong>not working for input type=[button]</strong>.</p>
  
  <pre><code>var validNavigation = false;
  
  function endSession() {
      document.cookie = 'MYOWNSESSID=; path=/';
  }
  
  function okEvent(){
       validNavigation = true;
    }
  
  function wireUpEvents() {
  
    window.onbeforeunload = function() {
        if (!validNavigation) {
           endSession();
        }
    }
  
    $("a").bind("click", okEvent );
  
    $("form").bind("submit", okEvent );
  
    // that's my adds that don't work
    $(":button").bind("click", okEvent );
    $(":button").bind("submit", okEvent );
    $(":button").onclick= okEvent;
  }
  
  
  // Wire up the events as soon as the DOM tree is ready
  $(document).ready(function() {
      wireUpEvents();
  });
  </code></pre>
  
  <p>The key if to flag an event as valid before unloading occurs. I'm trying to hook the buttons click but without success. The jquery selector is working fine, i checked that $(':button').length is > 0. 
  Sure i'm missing something but can't find it.</p>