How to know when a user has left the page and refreshed the page
<p>I want to make an AJAX call before the user leaves the page (so basically before leaving the page and before refreshing the page)?</p>

<p>How can this be done. I was trying to search something with jQuery but didnt get anything. </p>

<p>I tried to use the following code -</p>

<pre><code>window.onbeforeunload(function(){alert('before unload');});
</code></pre>

<p>But the alert box never appears when leaving the page(closing the browser tab) or refreshing the page. </p>

<p>How can this be accomplished?</p>
