Is there any way to know whether the parent window is closed or not in django view?

415 views Asked by At

I have a django view which is supposed to return HttpResponse depending on the status of the parent window which created the current window. If the parent window is still open, then I want to close the current window and redirect the parent window to some url, else if the parent window is closed then I want to redirect the new window to that url. Is there any way to determine this in my django view?

I tried this in Javascript

if (!window.opener.parent.closed){
    window.close();
    window.opener.parent.location.href="<url>";
}
else{
    window.location.href="<url>";
}

But the else block is not executed even if the parent window is closed, the if block works fine if the parent indow is open.

1

There are 1 answers

0
Zack Tanner On BEST ANSWER

If you are dealing with a window that popped up a new window, you should be checking for window.opener.

I would try the following code:

if(window.opener != null && !window.opener.closed) {
  window.opener.location.href="<url>"
  window.close()

}

You can put the else in a timeout, as the parent window can be closed at any time without the child knowing.

var intervalID;

function checkParent() {
    if (window.opener && window.opener.closed) {
        window.clearInterval(intervalID);
        window.opener.location.href="<url>"
    }
}
var intervalID = window.setInterval(checkParent, 500);