0

Alright, I am working on a site that displays third party developed courses, so I can't change anything in the actual pop-up window at all and the redirect call has to come from the parent window. The parent, child, and the redirectLocation are all on the same server, so that shouldn't be an issue. Here is the code I want to work.

    var new_window = window.open('courseAddress.php')

    new_window.onbeforeunload = function(){
        popupClosing(); 
    }

    function popupClosing(){
        window.location ="redirectLocation.php";
        alert('The popup has closed!');
        return false;
    };

The alert is firing, but the parent window doesn't seem to refresh. If I put the window.location ="redirectLocation.php"; outside of the .onbeforeunload the refresh works just fine.

I am sure I am missing something simple. Thank you in advance. I have read the other issue like this on here, however they were able to use window.opener.loactionfrom the child which unfortunately I am unable to do.

I have found a way that works, buuuuuuuut it's really nasty. I just have the parent window keep checking to see if the child window is there. Here is the fiddle for it: http://jsfiddle.net/ex9e7uLq/

Thanks to Eclecticist for sticking with me on this one too!

7
  • So you just want to refresh the parent window? You could try location.reload() instead of window.loaction = ....
    – user3886234
    Aug 19, 2014 at 20:07
  • Well, I need to redirect the parent window to a new location. However, it is interesting that location.reload() seems to be working fine, so thanks for the suggestion. However, I need a redirect too. So, reload is working fine, but anything I've tried to redirect seems to be failing. Aug 19, 2014 at 20:17
  • I'm on a tablet atm, so I can't test this. I think it may be a possibility that window is referring to the currently open child window, so your initial redirect is actually trying to redirect the child page as it closes. You might be able to create a global var to refer to the parent window, or perhaps create a custom event.
    – user3886234
    Aug 19, 2014 at 21:54
  • Something else to try is location.replace() .
    – user3886234
    Aug 19, 2014 at 22:04
  • Well, I've tried creating a global var to use. Still nothing. As a test I had it alert out it's location and it was the parent window and not the child's location. I also tried location.replace() with the same result. I appreciate your help on this. I'm just at a loss as to why it is not working. Aug 20, 2014 at 19:44

2 Answers 2

1

I've tested this on Chrome, Firefox, and IE all with no problems. Here is an example JSBin.

var win = window.open("http://www.google.com");

var winClosed = setInterval(function () {

    if (win.closed) {
        clearInterval(winClosed);
        location.assign("http://www.bing.com/");
    }

}, 250);

From your question, I know you wanted to attach an event handler to the child window. However, while testing different methods to do just that, I ran across a plethora of problems. Generally, Internet Explorer would fail to register any event in the child window from the parent window. Firefox and Chrome would act as if the event fired (regardless of the type) as soon as the child window launched.

I've read other SO questions with answers that apparently worked by doing what you want, but personally could not get them to work. Good luck :)

0

try this:

var new_window = window.open('courseAddress.php');

var intr = setInterval(function(){
  console.log(new_window.closed);
  if(new_window.closed){
    clearInterval(intr);
    window.location='http://www.google.com';
  }
}, 200);
1
  • 1
    Give some more details
    – E-Riddie
    Aug 21, 2014 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.