从父级(打开器)检测新的标签关闭 [英] Detect new tab closing from parent(opener)

查看:77
本文介绍了从父级(打开器)检测新的标签关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多帖子,但似乎都没有一个回答我确切的问题。

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

是否有可能检测到已打开的标签关闭通过 target = _ blank attr吗?

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.

有什么想法吗?

编辑:

进一步的解释

我有一个可以在iframe中打开另一个应用程序的应用程序。 iframe中打开的应用程序将连接到社交媒体以发布共享。新打开的标签用于身份验证/ oauth跳舞。在主窗口中打开的页面会显示已连接的帐户,并允许您连接新帐户(在iframe的主窗口中)。身份验证/ oauth打开一个新的窗口/选项卡进行跳舞,然后关闭自身。我需要父窗口(其中包含iframe的主窗口)知道新的标签页/窗口已关闭,以便它可以刷新内容并反映新的连接。

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.

我只需要找到一种监控方法新打开的选项卡/窗口,甚至有新的窗口/选项卡都会触发浏览器事件onunload,使父/主窗口知道它正在关闭,以便刷新。

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.

推荐答案

因此,如果有人感兴趣,这就是我现在如何使用它,直到找到更好的解决方案为止。

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

首先我确实是通过将javascript分配给变量来打开窗口的,因此它具有参考名称。

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);
}

这也将超时设置为在时每秒运行一次checkWindow == true 检查窗口是否关闭。
如果关闭了窗口(身份验证完成后我的脚本关闭了窗口),那么页面将重新加载,最终将 checkWindow 设置为false。

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.

这篇关于从父级(打开器)检测新的标签关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆