title: Log User Out Only on browser or tab close
body: <p>Hi There im looking for a way to log a user out of my site <strong>ONLY</strong> if they close the tab or browser.</p>
  
  <p>At the moment im just trying to alert a message on tab close or browser close but cant find a solution anywhere.</p>
  
  <p>I've done research and come across onbeforeunload, however it alerts every time you try to navigate to a different page in the site. </p>
  
  <p>i need to be able to navigate through the site and perform functions and only perform an action on close.</p>
  
  <p>Current code (alerts message every time you navigate)</p>
  
  <pre><code>&lt;script&gt;
    function closeIt()
    {
        if (window.closed) {
            return "Any string value here forces a dialog box to \n" +
                 "appear before closing the window.";
        }
    }
    window.onbeforeclose = closeIt;
  &lt;/script&gt;
  </code></pre>