how to check users leave a page
<p>I'd like to know that a user leave from a Web page. I think there might be three scenarios: 1. move to another page in the same Web site, either open a new window or load a new page to the current window, 2. move to a page in different site, and 3. close the window.</p>

<p>Based on these scenarios, what I'm thinking is to check whether the current window is the focus. So my first question is Is this the right way? And the second question is How should I implement it in javascript?</p>
