Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.opener is useless after a redirect in the popup window (JavaScript)

I'm building an app that involves authentication via third-party. To make the process not redirect the actual app I open a new window that then does the authentication and returns to main window after success.

This doesn't, however, go as well as planned. When the popup redirects to third-party and back, window.opener gets null. It's still possible to close the popup by window.close() but I also need to refresh the logged-in-area in the main window, like this:

window.opener.check_auth_status();

I really hope there is a way to fix this, e.g. binding a function to popup-close in the main window? Refreshing the whole page would be highly unnecessary.

One way is to set an interval to main window checking if the popup is closed, but this seems so fiddly.

like image 295
Martti Laine Avatar asked Jan 09 '12 05:01

Martti Laine


1 Answers

You have a few options that may or may not work in the latest versions of the browsers due to security updates

1) check that the window is closed from the opener - not fiddly and actually the safest

2) give the opener a name

window.name="myMainWindow";

and in popup (script from SAME domain) - should normally not open a new window or change content

var handle = window.open("","myMainWindow"); 
handle.check_auth_status();

3) use an iFrame in the popup and when you want to access the opener, use top.opener

like image 99
mplungjan Avatar answered Oct 28 '22 02:10

mplungjan