Link to home
Start Free TrialLog in
Avatar of HamidHossain
HamidHossain

asked on

Action When Closing the Current window

I want to make an action when the user close the current opened window by clicking the x button of the window I am using the follwoing code:

<head>
<script language="JavaScript">
function checkid(id)
{
 if((id !='') && (window.closed == true))
 {
   alert("Thank you");
  window.location="deluser.php?del_id="+id;
   return true;
 }
 else
 return false;
}
</script>
<?php
$LogId = 1955;
 ?>
</head>

<body onUnload='return checkid(<? echo "$LogId"; ?>);'>
Close the window
</body>

I want to call   window.location="deluser.php?del_id="+id; only when the window is closed by the user ...

Thank you
Avatar of archrajan
archrajan

U cannot open another url in the main window when the user closes the main window.
you can at the most open a new browser window with ur url.
<script language="JavaScript">
var flag
function checkid(id)
{
 if((id !='') && (flag != "yes"))
 {
   alert("Thank you");
  window.open("deluser.php?del_id="+id;)
   return true;
 }
 else
 return false;
}
</script>

and in all ur other internal links u shud have

<a href="sompeage.html" onclick="javascript:flag='yes';return true;">link</a>
Avatar of HamidHossain

ASKER

i think i did not pass my problem in the right way....
I want to call another page or do some action only when the user close the current active window  by clicking X button  
yeah the above code will be called in the unload event which is trigerred when the user clicks the x button,

the unload event also triggers when the user clicks some links in our page and navigate to other pages, to prevent that we set a the flag to yes and in our script check if the flag is not yes and only then open the url.
ASKER CERTIFIED SOLUTION
Avatar of archrajan
archrajan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zvonko
There is no way to recognize that a browser window is closing.
There is a workaround which works only for IE browser to see in the onunload event handler wether the screenTop is getting too high:
<script>
var id="? echo "$LogId"; ?>";
function pop(){
    alert(screenTop)
    if(screenTop>8000){
       window.open("deluser.php?del_id="+id,"pop","height=250,width=250");
    }
}

window.onunload = pop;
</script>

ok ... I want to do the following..
when the user close the window I want to call a PHP code that changes a  value in the DB ... this change should be done only if the user close the window because it is linked to the session destroy of the current online user ...
So I do not want to make this action using any link or button or form filed  ... I am trying to find a way to make  it on the window closed property
An alternative solution could be to have a hidden image where a PHP file is the source. This image could be refreshed every 60 seconds. Then if someone doesn't refresh for a longer period of time, you know that their session isn't alive anymore. Then a server side script could from time to time delete logged out users.

Example of the hidden image:

<img id="myImage" src="keepAlive.php" style="display:none">
<script>
function refreshImage(){
    document.getElementById('myImage').src='keepAlive.php?random='+Math.random();
    setTimeout('refreshImage()',500);
}
setTimeout('refreshImage()',60000);
</script>
What is the screenTop property?
setTimeout('refreshImage()',500);

should be

setTimeout('refreshImage()',60000);

which is 1 minute.
The property window.screenTop contains the offset of the upper border of the window from the upper left screen corner in pixel.
The property window.screenLeft containst the offset of the left window border to the screen border.

The workaround is, that at close event execution time this screenTop property is unnaturaly heigh in IE. In an page unload event tat screenTop is still the window offset to upper screen border.
<html>
<head>
<script>
//navigete when close a window
window.onbeforeunload =
  function(){
    if((window.event.clientX<0) || (window.event.clientY<0)){
               window.open("the page u want");
            }
  }
</script>      
</head>

<body>
</body>
</html>
******************************************
in the page u want

//do whatever u want
//at end of the file put this:
<script>
window.close();
</script>


aaaaaa, the event onbeforeunload fires only in IE, and there it gets negative values for window.event.clientY also at page refresh, at list in my IE6.0 on XP.
oh, for me refresh didn't perform anything.

and i only know IE.
You live in a perfect world ;-)
using ((window.event.clientX<0) || (window.event.clientY<0)) does not work 100% all the times ... can I modify this condition or add another one to ?
did you try something like this :
<script language="JavaScript">
function do_quit(id) {
   var i = new Image();
   i.src = "deluser.php?del_id="+id;
   or just window.location="deluser.php?del_id="+id; // not sure here
}
</script>

<body onbeforeunload="do_quit()" onunload="do_quit()">
<img src="logout.jpg" onclick="if(confirm('Are you sure you want to quit?')){do_quit();parent.location=''}" title="Close">
</body>

the idea is to readapt the scripts from cgi chat systems wich disconnects you when you close the window
for more details check how this works : http://cgiirc.sourceforge.net/
example of a page using such kind of onclose events : http://webchat.helioschat.org/irc.cgi

I had the same problem with one of my scripts. The only solution that I found was to open a new  
small window that checks if the parent window still exists. If so closes itself, else redirect to the desired php page that closes itself.


<body onunload="javascript: window.open('check_parent_window.html','','width=1,height=1')">

the check_parent_window.html would look something like this:
<script>
if (window.opener && !window.opener.closed)
{
//alert("opened");
top.close();
}
else
{
//alert("closed");
document.location.href=='desired_php_script.php';
}
</script>

Wolfram
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: archrajan

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

cem_turk

EE Cleanup Volunteer