0

我有这个代码:

<html><head></head><body>
    <script language="javascript" type="text/javascript">
        function f(){
             //WHAT I HAVE TO PUT HERE?
        }
        function unloadFCT() {
             //CODE WHEN THE WINDOW IS UNLOADED
        }               
        var val = navigator.userAgent.toLowerCase();
        if(val.indexOf("msie") > -1){
             window.attachEvent('onbeforeunload', unloadFCT);
        }
        else{
             window.onunload = unloadFCT;   
        }
</script>
<a href="#" onclick="f()" >Call unloadFCT() function</a>
</body></html>

f() 的目标是执行 unloadFCT() 的代码。

谁能告诉我 f() 的代码是什么?

4

2 回答 2

1

/ * ** * ** * **** test1.php * ** * ** * *** /我创建一个简单的例子来理解问题,下面是test1.php文件的内容

<html><head></head><body>
<script language="javascript" type="text/javascript">
var xhrHttp;
function getXHRObject() {
   var obj = false;
   try {
      obj = new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch (e) {
      try {
         obj = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
         try {
            obj = new XMLHttpRequest();
         }
         catch (e) {
            return false;
         }
      }
   }
   return obj;
}
function unloadFCT() {
    xhrHttp = getXHRObject();   
    xhrHttp.open("GET", "ajaxCall.php", false);
}       

var val = navigator.userAgent.toLowerCase();
if(val.indexOf("msie") > -1){
    window.attachEvent('onbeforeunload', unloadFCT);
}
else{
    window.onunload = unloadFCT;    
}

当您关闭此窗口时,将在 ajaxCall.php 所在的位置创建一个新文件

/ * ** * ** * **** ajaxCall.php * ** * ** * *** /

<?php
    $handle = fopen("myFile_".time().".txt", 'a+');
    fwrite($handle, "simple message..."); 
?>

/ * ** * ** * **** test2.php * ** * /

<html><head></head><body>
<script language="javascript" type="text/javascript">
var xhrHttp;
function f(){
    unloadFCT();
    window.close();
}
function getXHRObject() {
   var obj = false;
   try {
      obj = new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch (e) {
      try {
         obj = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
         try {
            obj = new XMLHttpRequest();
         }
         catch (e) {
            return false;
         }
      }
   }
   return obj;
}
function unloadFCT() {
    xhrHttp = getXHRObject();   
    xhrHttp.open("GET", "ajaxCall.php", false);
}       
var val = navigator.userAgent.toLowerCase();
if(val.indexOf("msie") > -1){
    window.attachEvent('onbeforeunload', unloadFCT);
}
else{
    window.onunload = unloadFCT;    
}       

执行 unloadFCT() 函数的代码并关闭此窗口!!!

/ * ** * ** * ** * ** *问题* ** * ** * ** * * /

你能告诉我test2.php有什么问题吗?我希望 f() 函数执行 unloadFCT() 的代码并关闭当前窗口。

于 2012-04-19T11:35:54.743 回答
0

你应该在之后添加 window.close()unloadFCT();

于 2012-04-19T13:16:03.167 回答