Monday, June 02, 2008

Check if a Child Window is closed using Javascript

The basic idea here is to have a javascript timer, that checks to see if the child window is closed. The setTimeout function in javascript takes a function as input and runs at a specified interval. In the function we give as input to the timer, we check to see if the popup window is closed, if closed do what ever you want to do if the popup is closed and stop the timer.

<script language="javascript">

var detailsWindowTimer;
var detailsWindow;

function OpenDetailsWindow()
{
detailsWindow = window.open('../../UserDetails.aspx?UserID=1', ' 'UserDetails','directories=0,location=0,toolbar=0,status=1,menubar=0,scrollbars=1,resizable=1');
detailsWindow.focus();
detailsWindowTimer = setInterval("WatchDetailsWindowForClose()",2000); //Poll
every 2 seconds to see if the details window is open or closed
}

function WatchDetailsWindowForClose()

{
if (!detailsWindow || detailsWindow.closed)
{

/// Do your stuff here....
clearInterval(detailsWindowTimer); //stop the timer
}
}
</script>

<a href="#" onclick="OpenDetailsWindow()">Details</a>

Digg It! Add to del.icio.us Stumble This

1 Comment:

Greg Abernethy said...

Hi

To have the timer loop you need to use setInterval and clearInterval as setTimeout only fires once.