• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2145
  • Last Modified:

Capuring close event of Popup window.

I have a pop-up window that keeps refreshing every 10 seconds.If it finds that its opener has been closed ..it goes to the logout page and logs out the user.I have accomplished this task.If the user will closes the opener window, then the user is automatically logged out.

Now,if the user closes the pop-up window and then the opener window,I have no control.
What I want to accomplish is this: If the user closes the pop-up window while its opener is still present ,the user must be logged out.I need to capture the close event correctly for this.My problem is if I put the redirection to the logout page on the window's unload  event,it is a problem ..as the unload event gets triggered even when the page is being refreshed.and this pop-up is refreshed every 10 seconds!!

Restriction : I can't change all pages of my application..that is why a pop-up window is opened when the user logs in and it keeps track of the user closing the opening window.

I have a good link on controlling windows using Javascript ..but am still unable to do this.The link is this : http://developer.irt.org/script/window.htm.

Any help/sugestion will be welcome.
0
hpriya78
Asked:
hpriya78
1 Solution
 
knightEknightCommented:
when you open the window, store the reference in a global variable:

<SCRIPT>
var mypopup;  // global to hold the popup handle

// later, when you open the popup:

  mypopup = window.open( url, "mypopup", "width=800,height=600" );

// then every 10 seconds poll to see if the popup still exists, if it doesn't, then redirect to logout:

setInterval( "if(!mypopup || mypopup.closed==true)location='logout.jsp';", 10000 );


0
 
alambresCommented:
you can set a flag in the function called in the timeout, and then check it in the onunload. I mean:

supposing :

var bReloading = false;  //must be global


function refreshWin()
{
        // just need to add:
        bReloading = true;
        // your code, to reload and so
}

then to avoid the function logging out the user ( we'll suppose logOut() ) when just refreshing :
<body onunload="if(!bReloading) logOut();">

I think it will do the work

alambres
0
 
hpriya78Author Commented:
hi KnightEKnight,

I have already accomplished what u have said as I mentioned in my query.What I actually need is a method by which I can capture the close event of the popp-up window.My main application runs in the main large window.I have a popup window that checks for the existence of the main window..if the main window is closed ..it automatically logs off the user.This is fine and working..now waht I require is a method by which I can prevent/control the closing of the pop-up window.

Hi alambares ..I have tried it ..doen't work..am I doing something wrong? here is a copy of my pages..
My IE browser version is 6.0.2800.1106 with SP1..I need to make this work only for IE6 and above as this is going to be used only internally.

//Main Page Script that calls the pop-up

<script>
childwin=window.open("test1.asp", "test1", "width=100, height=100, left=0, top=0,menubar=no, status=no, location=no, toolbar=no, scrollbars=no, resizable=no");
self.focus();
</script>

//Popup page

<html>
<head>
<script>
<!--

var limit="0:10"
var bReloading=false

if (document.images){
var parselimit=limit.split(":")
parselimit=parselimit[0]*60+parselimit[1]*1
}

function beginrefresh()
{
bReloading = true;
if (!document.images)
return

if (parselimit==1)
{
      window.location.reload()
}
else
{
      parselimit-=1
      curmin=Math.floor(parselimit/60)
      cursec=parselimit%60
      if (curmin!=0)
            curtime=curmin+" minutes and "+cursec+" seconds left until page refresh!"
      else
            curtime=cursec+" seconds left until page refresh!"
      window.status=curtime
      setTimeout("beginrefresh()",2000)
      
      
      if (window.opener.closed)
      {
      redirURL = "logout.asp";
      self.location.href = redirURL
      }
      
}

}
function closewin()
{
      //alert("hi")
      window.open("logout.asp","logout","")
}

window.onload=beginrefresh
//alert(top.opener.closed)
//-->



</script>



</HEAD>

<BODY onunload="if (!bReloading){closewin();}">
<img src="images/midd1.jpg"  width="86" height="95">
<p>
</BODY>
</HTML>
0
 
ileanarcCommented:
Hi, hpriya78,

I might be wrong, but I think that in your page bReloading is always 'true', because on the page onload event you call the beginrefresh() function, which sets bReloading to 'true'.

Instead you should have sth. like:

bReloading = false;

function beginrefresh(){
     bReloading = true;
     window.location.reload()
}

function init(){
     curmin=Math.floor(parselimit/60)
     cursec=parselimit%60
     if (curmin!=0)
          curtime=curmin+" minutes and "+cursec+" seconds left until page refresh!"
     else
          curtime=cursec+" seconds left until page refresh!"
     window.status=curtime
     setTimeout("beginrefresh()",2000)  
     
     if (window.opener.closed)
     {
     redirURL = "logout.asp";
     self.location.href = redirURL
     }
}

window.onload=init

The init() function will be called on the onload window event, setting the window timeout, and the beginrefresh() function will handle the page reload when the timeout expires.

Ileana.
0
 
hpriya78Author Commented:
Well,I got a great script that will help me detect  the close event of my popup window.

if(window.screenTop>10000)
      {
            if (!window.opener.closed)
            {
                  window.opener.location="logout.asp"
            }
            else
            {
                  window.open("logout.asp","logout","")
            }
      }

Well, I got this from a a site.And since I borrowed code from alambres i'll accept it as I am using some part of his code although that does not provide a complete solution.

Ilearnarc: I am not sure about this ..I haven't tried it ..I have used the bReloading part exaclty as specified by alambres..perhaps u could ask alambres.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now