Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to Child window close event in Oauth Like Situation [ jQuery| Javascript ]

I am using oAuth 1.0 to access 3rd party data.

For this I am taking following steps :-

  1. User clicks on "sync with 3rd party" button. ( It is necessary for user to click because this usually doesn't invoke popup blocker.)

  2. When user click on this button following code is executed :

        $('#sync_with_thirdparty').click(function(){
           var child = window.open('/oauth');
           $(child).unload(function() {
               if (this.location != "about:blank") {
                alert("Child window closed");
            }
          });
        });
    
  3. Now in the new window PHP get the authentication URL from 3rd party then it redirects the user to the same URL. As soon as user redirects to 3rd party above code says that "child window closed.".

  4. Then user makes himself login on third party site (if necessary) and gives access to our app. As soon as he clicks on allow button third party redirects the user to a new page in our website. On this page I am calling window.close() method which is working perfectly fine.

  5. But upon window.close() in child window, parent window is not able to know that child is closed.

How I can solve this problem?

Note : This 3rd doesn't like Iframe and refreshes itself to open in top window. I don't have any control over 3rd party's content. Our webpage is in HTML5 format.

like image 362
techgyani Avatar asked Nov 13 '22 11:11

techgyani


1 Answers

The child window can communicate with the parent window via the window.opener() function. You could do something like this:

Child window

window.opener.messageFromChildWindow('closed');

Parent window

function messageFromChildWindow(msg) {
    if(msg === 'closed') {
        alert('child window closed');
    }
}
like image 194
sica07 Avatar answered Nov 15 '22 06:11

sica07