title: Trigger an event when user navigates away
body: <p>I need to call a JavaScript/jQuery function which has a few lines of code in it, on a PHP page when the user closes his window/tab or navigates away by clicking a link. I've tried the <code>onbeforeunload</code> function but only the <code>return "blah blah blah;"</code> part executes and everything else is ignored. I've also tried the <code>.unload</code> method from jQuery but for some reason this code doesn't run.</p>
  
  <pre><code>$(window).unload(function() {
      alert('blah blah blah');
  });
  </code></pre>
  
  <p>Please suggest alternatives. Thanks..</p>