当前位置:编程学习 > JS >>

js监听事件

需要在浏览器关闭(浏览器程序关闭及浏览器标签关闭)时,使用异步方法向服务器端回发一些信息。
HTML code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>HTMLPage3</title>
    <script language="javascript" type="text/javascript">
    var xmlHttp; www.zzzyk.com
   
    function createHttpRequest()
    {
        if (!window.XMLHttpRequest)
        {
            window.XMLHttpRequest = function()
            {
                var progIDs = [ 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP.2.6',
'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'MSXML.XMLHTTP' ];
                for (var i = 0; i < progIDs.length; i++)
                {
                    try
                    {
                        xmlHttp = new ActiveXObject(progIDs[i]);
                        break;
                    }
                    catch (ex) { }
                }
            }
        }
        else
        {
            xmlHttp = new XMLHttpRequest();
        }
    }

    function doSearch()
    {
        createHttpRequest();
        var url = "myPage.aspx?id="
       
        if(document.getElementById("txt") != null)
        {
            url = url + escape(document.getElementById("txt").value)
        }
        else
        {
            url = url + "-1";
        }
       
        if(xmlHttp != null)
        {
            xmlHttp.open("GET", url, true);
            xmlHttp.send(null);
            //alert("Send Complete!");
        }
    }
   
    window.onbeforeunload = function()
    {
        doSearch(); 
    }
    </script>
</head>
<body>
    <h1>HTMLPage3</h1><br />
    <input type="text" id="txt" />
    <input type="button" id="btn" value="Click" onclick="doSearch();" />
</body>
</html>

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,