简体   繁体   中英

Redirect to Parent URL from callback after using window.open

I'm trying to redirect the callback URL back to the parent window that is calling window.open. Currently the callback URL is opening inside the child window spawned from the parent.

Parent Window => Opens child window for authentication => user authenticates and is redirected to this child window. How do I redirect to the Parent Window?

Here is the call to window.open

newwindow = window.open(response.data.auth_url,'Etsy','height=800,width=1000');
if (window.focus) {
    newwindow.focus()
}

Any idea how to accomplish this?

To communicate between windows use Postmessage

https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

Your scenario might be that the child window sends a message to the parent window that in turn closes the child window and redirects to the final url.

Here's the solution I came up with if anyone else runs into this issue.

In my angularJS Controller file I check for the two pieces of info I need from the callback URL:

if ($state.params.oauth_token && $state.params.oauth_verifier) {

    /* This closes the child window */
    $window.close();

    var params = {
        oauth_token: $state.params.oauth_token,
        oauth_verifier: $state.params.oauth_verifier
    };


    /* process the info ... */

}   

I also poll to see when the callback URL is made and redirect the parent window

win = window.open(response.data.auth_url,'Etsy','height=800,width=1000'); 

var pollTimer = window.setInterval(function() { 
    try {
        if (win.document.URL.indexOf(response.data.callback_url) != -1) {

            window.clearInterval(pollTimer);

            $state.go("etsy.connected");

         }
     } catch(e) {
           // Error Handling            
     }
}, 500);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM