简体   繁体   中英

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().

Who can tell me what's the code of f()?

/ * ** * ** * **** test1.php * ** * ** * *** / I create a simple example to understand the problem, Bellow is the content of test1.php file

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

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

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

/ * ** * ** * ** * ** * Problem * ** * ** * ** * * /

Can you please tell me what's wrong in test2.php? I want the f() function to execute the code of unloadFCT() and close the current window.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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