我现在在关闭窗口时要去执行一段代码,具体代码如下:   
    
  <script   language="javascript">   
      function   function1(){   
          alert("aaa");   
          window.open("test.jsp");   
      }   
  </script>   
  <body   onunload="javascript:function1();">   
  </body>   这样做没有效果啊。怎么才能实现在unload事件中调用javascript函数,在javascript函数中请求一个特定页面?

解决方案 »

  1.   

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head>
    <script language="javascript">var xmlDoc;
    var http_request=false;function initXMLHttp() {
            if (window.XMLHttpRequest) { // Mozilla, Safari,...
                http_request = new XMLHttpRequest();
                if (http_request.overrideMimeType) {
                    http_request.overrideMimeType('text/xml');
                }
            } else if (window.ActiveXObject) { // IE
                try {
                    http_request = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    try {
                        http_request = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e) {}
                }
            }
    if (!http_request) {
                alert("您需要升级您的浏览器!");
                return false;
            }
    }
    function function1(){
    initXMLHttp();
    var url = "http://xxx.xxx.xxx/xxx.jsp";
    http_request.open("POST",url , false);
    http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    http_request.send(null);
    }
    </script>
    <body onunload="function1()">
    </body>
    </html>
      

  2.   

    我用了xmlhttp对象,验证过了,是可以在后台给服务器发请求的
      

  3.   

    这样也可以<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head>
    <script language="javascript">
    function function1(){
    window.open("http://www.cslg.cn/");
    }
    </script>
    <body onunload="function1()">
    </body>
    </html>
      

  4.   

    我把你的代码拷贝到一个记事本里,然后改成htm,放到tomcat发布的目录下,打开该网页,然后在地址栏输入其他网站网址,可是毫无效果,改成 <body  onunload="alert('aa');">  就有效果,郁闷中....
      

  5.   

    第一个例子是在后台发送请求的,不打开页面,所以看不到,但是可以用sniffer工具检测
    第二个例子是在新窗口打开的,看看你的浏览器是不是过滤了弹出窗口
      

  6.   

    好像不行无论是用 document.location="..." 或者 window.open("...","_top")都会使浏览器中显示你请求的页面,而不是输入的新的地址