Home » Core Java » Show Alert on Parent Window When Child Window closes

Show Alert on Parent Window When Child Window closes

The best way to know when the child window closes from parent window is to keep polling the information about the child window at regular interval. Interval should be very low e.g. 500 milliseconds so that as soon as child window closes , parent window would come to know. And then, alert can be shown on parent window.

The below code explains as to how to do it using html and javascript.

function onClickButtonFunction(){
   var xWidth = screen.width;
   var yHeight = screen.height;
   var top = (yHeight - 600)/2;
   var left = (xWidth - 600)/2;
   var attr = "width=600,height=600,toolbar=0,location=yes,top="+top+",left="+left;
   myWindow = window.open("","newWindow",attr);
   childWindowTimer = setInterval(checkChildWindow, 500);
}

Here , in the above code, when the child window is opened, it has a reference “myWindow”. Using this, we can check at regular intervals whether the child window is closed or not using the below function.

function checkChildWindow() {
    if (myWindow.closed) {
       clearInterval(childWindowTimer);
       alert("Child Window is closed");
    }
}