Saturday, October 22, 2011

Child window (popup) close event in parent window

This is the most useful and most wanted code for raising window close event of child window from parent window.

<script type="text/javascript">
   var childWindow = null;
   var timer;
   var flag = 1;
   function openChildWindow() {
       childWindow ='child.htm', 'My popup', 'width=400,height=250');
   function checkChildWindowStatus() {
       if (childWindow.closed) {
           if (flag == 1)
                flag = 0;
           else {
                timer = 0;
                flag = 1;
   window.onload = function () {
       timer = setInterval('checkChildWindowStatus()', 1000);

In the above code childWindow is the variable which reference to the popup window. Using this window status can be identified.

checkChildWindowStatus() is the function where the popup window status is checking and based on this some action (page refresh) can be performed in parent window. This function is getting called for frequent interval of time using serInterval() method.

Happy coding…

No comments:

Post a Comment