简体   繁体   English

如何卸载当前窗口?

[英]How to unload the current window?

I have this code: 我有以下代码:

<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>

The goal of f() is to execute the code of unloadFCT(). f()的目标是执行unloadFCT()的代码。

Who can tell me what's the code of f()? 谁能告诉我f()的代码是什么?

/ * ** * ** * **** test1.php * ** * ** * *** / I create a simple example to understand the problem, Bellow is the content of test1.php file / * ** * ** * **** 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;    
}

When you close this window, a new file will be created in the same place as ajaxCall.php 当您关闭此窗口时,将在与ajaxCall.php相同的位置创建一个新文件。

/ * ** * ** * **** ajaxCall.php * ** * ** * *** / / * ** * ** * **** ajaxCall.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;    
}       

/ * ** * ** * **** test2.php * ** * / / * ** * ** * **** 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; } 

Execute the code of unloadFCT() function and close this window!!! 执行unloadFCT()函数的代码并关闭此窗口!!!

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

Can you please tell me what's wrong in test2.php? 您能告诉我test2.php怎么了吗? I want the f() function to execute the code of unloadFCT() and close the current window. 我希望f()函数执行unloadFCT()的代码并关闭当前窗口。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM