How do I call a function on confirmation of leaving page?
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/1299452/how-do-i-stop-a-page-from-unloading-navigating-away-in-js">How do I stop a page from unloading (navigating away) in JS?</a>  </p>
</blockquote>



<p>I've created an online whiteboard. I wish to confirm from user whether he wants to leave the page or not (since he needs to save whiteboards before leaving)<br/></p>

<pre><code>$(window).bind('beforeunload', function(){
    return 'Are you sure you want to leave?';
});
</code></pre>

<p>Now I need to call a function if the user confirms to leave the page. That function will post to the DB from jQuery (which I know how to do) &amp; convert the user from active to inactive.
<br/>
Only trouble I'm having is <b>how do i get that function to be executed when the user CONFIRMS to leave.</b> 
<br/>
Thanks!<br/></p>

<hr>

<p>FURTHER...<br/></p>

<pre><code>$(window).bind('beforeunload', function(){
  return 'Are you sure you want to leave?';
});

window.onunload=pageleave();
function pageleave() {
    var grp=$("#grpid").val();
    var login=$("#loginid").val();
$.ajax({
    type: 'POST',
    url:'collwb_pageleave.php',
    data: { GrpID:grp, LogID:login },
    cache:false,
});
}
</code></pre>

<p><br/>
Here what happens is, on page load, pageleave gets executes and make Activity=0 in the Database. This should happen on page unload... Why? Why would this happen?</p>
