title: Differentiate browser refresh and browser close
body: <p>I want to set a cookie when a visitor on the page closes the browser.
  I used onbeforeunload method like this</p>
  
  <pre><code>&lt;script language="JavaScript" type="text/javascript"&gt;
    window.onbeforeunload = confirmExit;
    function confirmExit()
    {
      return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
    }
  &lt;/script&gt;
  </code></pre>
  
  <p>followed this <a href="https://stackoverflow.com/questions/333665/javascript-to-get-an-alert-when-closing-the-browser-window">link</a> </p>
  
  <p>But found out that even refresh of page or click on any hyper link on the page,pops up an alert.</p>
  
  <p>I need to set cookie only when visitor clicks cross button of browser and not on page refresh.</p>
  
  <p>I followed some of the links like the one above and another <a href="http://www.webdeveloper.com/forum/showthread.php?202588-refresh-or-close" rel="nofollow noreferrer">here</a>. And found from the post that we can not differentiate between close and refresh. But those posts were 3-4 years back.</p>
  
  <p>Is there is any way to differentiate these events using JavaScript or Jquery?</p>