Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect new tab closing from parent(opener)

I have read a bunch of posts but none of them seem to answer the question that i have exactly.

Is it possible to detect a tab closing that was opened by a target="_blank" attr?

I need a new tab/window to open, the user will select an option, then the tab closes.

When that tab closes i need the original window(parent or opener) to refresh.

Any ideas?

EDIT:

further explaination

i have an application that opens another application in an iframe. the application opening in the iframe connects to social media for posting a share. the new tab that opens is used for authentication/oauth dance. the page that is open in the main window shows the accounts that are connected and allows you to connect new accounts(in the main window in the iframe). the authentication/oauth opens a new window/tab to do the dance, then it closes itself. i need the parent window(main window with iframe in it) to know that the new tab/window closed so it can refresh the content and reflect the new connect.

FINAL NOTE I use many authentication methods, so really the above is irrelavant.

I just need to find out a way to monitor the new tab/window that has opened or even have the new window/tab fire a browser event onunload to that the parent/main window knows it closing so it can refresh.

like image 386
JD Vangsness Avatar asked Oct 15 '25 04:10

JD Vangsness


1 Answers

So if anyone is interested, this is how I have it working for now until I find a better solution.

First what I did was had javascript open my window by assigning it to a variable so it had a reference name.

var checkWindow = false;
var fbAuthWindow;

function openFBAuth(url) {
    fbAuthWindow=window.open(url);
    checkWindow = true;
    setTimeout(function() { checkAuthWindow('fb'); }, 1000);
}

That also sets a timeout to run every second while checkWindow == true to check if the window is closed. If the window is closed (my script closes the window when authentication is complete), then the page reloads ultimately setting checkWindow back to false.

function checkAuthWindow(win){
    if(checkWindow == true){
        if(win == 'fb'){
            if(fbAuthWindow.closed){
                window.location.reload();
            } else {
                setTimeout(function() { checkAuthWindow('fb'); }, 1000);    
            }
        }       
    }
}

It isn't the prettiest or by any means a best practice to have the checking the window every second, but for now it will work until i find a better solution.

like image 130
JD Vangsness Avatar answered Oct 17 '25 16:10

JD Vangsness