1

The situation is like this. With JavaScript I open a window with a URL on my domain showing a loading indicator.

After about a second, the URL of the window is changed to a PayPal window (so other domain). I want to be able to capture the event when the user closes the paypal window, so I can redirect the user to the correct page saying the payment failed.

I encountered window.onbeforeunload but that one is fired when the url changes to PayPal, but not when the window is closed after that. How do I overcome this issue?

So, this doesn't seem to be working, and I can't find any other answers around here:

    ppwin = window.open('paypal/loading.php', 'paypal_popup', 'width=900,height=500');
    ppwin.onbeforeunload = function(){
        //triggered when URL changes
        if (ppwin.closed) // false because window is still open.
            // Order failed
    }

Even though I accepted the answer, I am still not statisfied with it. It works, but thats it. If someone provides a better answer I would be much more happy!

Rene Pot
  • 24,681
  • 7
  • 68
  • 92

2 Answers2

2

I came accross the same problem and in the end what I did is to make the parent window periodically check if the popup window was closed. Any alternative solution drove me nuts.

The code could be like this:

// Create popup window
var popup = window.open('http://www.google.com');

// Set interval to check if it has been closed
var windowCheckInterval = setInterval(function() { windowCheck(); }, 300);

// Check function
function windowCheck() {
    if (!popup || popup.closed) {
        clearInterval(windowCheckInterval);

        // Do something
    }
}

Despite it is quite a rude solution, user experience is quite good and programming is really simple.

You must remember to clear the interval when process finishes in a different way (not just when the window is closed), it there is any.

German Latorre
  • 10,058
  • 14
  • 48
  • 59
  • Alright, I thought of that too, but I don't really like it. "Any alternative solution drove me nuts.". What alternative? – Rene Pot Jan 22 '13 at 12:34
  • I tried `popup.onclose`, `popup.onunload`, ... But none of them worked. Also you cannot control the behaviour of the popup, as it is not your code (you mentioned that PayPal was opened there). Maybe there are solutions better than setting an interval, but I know none. :-( – German Latorre Jan 22 '13 at 12:48
  • Thanks for this. This "seems" hacky, but it really does work perfectly. Kudos. – dmackerman Nov 21 '14 at 18:23
0

see Capture the close event of popup window in JavaScript

You can catch it as long as the pop-up window url is in the same domain as the parent. Otherwise try "the best you could do is open a document in your domain that then loads the remote URL in an iframe, or reads it in via server scripts and renders it from there. "

Community
  • 1
  • 1
roel
  • 2,005
  • 3
  • 26
  • 41
  • I kinda already said that it isn't the same domain. Therefore your answer remains useless – Rene Pot Jan 22 '13 at 12:53
  • you can say its useless because it doens't solve your problem. But if your problem is something that cannot be done in a specific tool/environment. Then answering that it can not be done, is a correct answer. Maybe accept security restriction in browser behaviour would be a proper solution. Otherwise you keep waiting and looking for an solution that doesn't exist; – roel Jan 22 '13 at 12:56
  • the point is, you answer a question with a contradicting answer. I said it is not the same domain, you give an answer with the same domain. – Rene Pot Jan 22 '13 at 13:12