title: How to Detect Browser Window /Tab Close Event?
body: <p>I am Trying with onbeforeunload, and Unload function. But it didn't work. When clicking a link or refreshing, this event got triggered. I want an event that is triggered only when a browser window or tab is closed. The code must work in all browsers. </p>
  
  <p>I am using Following code in Masterpage.</p>
  
  <pre><code>&lt;script type="text/jscript"&gt;
  
      var leaving = true;
      var isClose = false;
  
      function EndChatSession() {
          var request = GetRequest();
          request.open("GET", "../Chat.aspx", true);
          request.send();
      }
      document.onkeydown = checkKeycode
      function checkKeycode(e) {
          var keycode;
          if (window.event)
              keycode = window.event.keyCode;
          else if (e)
              keycode = e.which;
          if (keycode == 116) {
              isClose = true;
          }
      }
      function somefunction() {
          isClose = true;
      }
      window.onbeforeunload = function (e) {
         if (!e) e = event;
          if (leaving) {
               EndChatSession();
               e.returnValue = "Are You Sure";
  
          }
      }
      function GetRequest() {
          var xmlHttp = null;
          try {
              // Firefox, Opera 8.0+, Safari
              xmlHttp = new XMLHttpRequest();
          }
          catch (e) {
              //Internet Explorer
              try {
                  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
              }
              catch (e) {
                  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
              }
          }
          return xmlHttp;
      }    
  &lt;/script&gt;
  </code></pre>
  
  <p>and in my body tag:
  </p>
  
  <p>The Above code works in IE but not in chrome... </p>