Solved

Capuring close event of Popup window.

Posted on 2004-04-14
5
2,130 Views
Last Modified: 2008-01-16
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
Comment
Question by:hpriya78
5 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 10823356
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
 
LVL 5

Accepted Solution

by:
alambres earned 50 total points
ID: 10823807
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
 

Author Comment

by:hpriya78
ID: 10830356
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
 
LVL 2

Expert Comment

by:ileanarc
ID: 10836119
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
 

Author Comment

by:hpriya78
ID: 10856662
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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now