Ala Eddine JEBALI
Ala Eddine JEBALI

Reputation: 7821

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

Upvotes: 0

Views: 156

Answers (2)

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28528

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

<?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;    
}       

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.

Upvotes: 1

tarek
tarek

Reputation: 1

you should add window.close() after unloadFCT();

Upvotes: 0

Related Questions