原生JS ajax 判定浏览器是否关闭

    <script language="javascript" type="text/javascript">
      //这个函数是一直固定不变的,你只需要调用它,检验是否能创建 XMLHttpRequest对象
      var xmlHttp;  
      function createXmlHttpRequest() {     
        if (window.XMLHttpRequest) {
          xmlHttp = new XMLHttpRequest();

          if (xmlHttp.overrideMimeType) {
            xmlHttp.overrideMimeType("text/xml");
          }
        }
        else if (window.ActiveXObject) {
          try {
            alert("测试成功");
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
          }
          catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
          }
        }
        if (!xmlHttp) {
          window.alert("你的浏览器不支持创建XMLhttpRequest对象 \n- 请换用IE 或 火狐");
        }
        return xmlHttp;
      }
      function ChangeSessionID()
      {
        var id = '<%=Session["UserID"] %>';
     
        if (!createXmlHttpRequest())
        {
          alert('程序执行出现错误。');
        }
        else
        {
          var xmlHttpTest = createXmlHttpRequest();
          var url = "Handler1.ashx?value=" + id;
          xmlHttpTest.open("get", url, true);
          xmlHttpTest.send(null);
         }
      }
      function close() {
        if (document.body.clientWidth - event.clientX < 170 && event.clientY < 0 || event.altKey) {
          ChangeSessionID();
        }
      }
      window.onbeforeunload = close;
    </script>

原文地址:https://www.cnblogs.com/xiaozhuaweiliang/p/ajax.html