Detect new tab closing from parent(opener)

人走茶凉 提交于 2019-12-22 11:26:25

问题


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.


回答1:


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.




回答2:


Here is the working example (http://jsfiddle.net/sureshpattu/w8x7dqxr/) and code

Html

<button class="openPopActBtn">Open pop window </button>

Javascript

var checkWindow = false;
var fbAuthWindow;
$('.openPopActBtn').click(function() {
  openFBAuth('www.google.com');
});

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

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


来源:https://stackoverflow.com/questions/24592827/detect-new-tab-closing-from-parentopener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!