如果父刷新,则检索对子窗口的引用 [英] Retrieving references to child window if the parent refreshes

查看:95
本文介绍了如果父刷新,则检索对子窗口的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面(parent.html),其中有许多链接,点击后会打开一个窗口:

I have a page (parent.html) which has many links which on clicking opens a window as:

winRef = window.open(url, '_blank', 'width=800,height=600,resizable,scrollbars');

要在子窗口关闭时发出警告,请在parent.html中使用以下内容。

To give a warning when the child window is closed I use the below in parent.html.

var timer = setInterval(function() {   
        if(win_ref.closed) {  
            clearInterval(timer);  
            alert("window closed");
            console.log("name is"+name);
            list.remove(name);  
        }  
}, 1000);

这很好用。如果子窗口关闭,则会警告用户。但是如果父窗口被刷新,那么它不会警告用户,原因是在那种情况下winRef被重新初始化。

This works fine. This warns user if the child window is closed. But if the parent window is refreshed then it doesn't warn the user, reason is that in that case winRef is reinitialized.

为了处理它我使用了cookie,但它是不工作我在下面的代码中遗漏/做错了什么?

To deal with it I used cookies but it's not working. What am I missing/doing wrong in the below code?

var winRef;
var list = new cookieList("cookie_for_winRef");
var list_items = [];
var win_refs = new cookieList("winrefs");
var win_refs_items = [];
var index;
list_items = list.items();
function open_online_editor(name) {
    index = list_items.indexOf(name);
    if (index > -1) {
        //list.remove(name);
        alert("Window is already opened");
        return;
    }
    var url = '$action?command=dbot_show_online_editor&name='+name;
    if (typeof (winRef) == 'undefined' || winRef.closed) {
            //create new, since none is open
            console.log("in if");
            winRef = window.open(url, '_blank', 'width=800,height=600,resizable,scrollbars');
    }
    else {
            try {
                winRef.document; //if this throws an exception then we have no access to the child window - probably domain change so we open a new window
            }
            catch (e) {
                winRef = window.open(url, 'width=800,height=600,resizable,scrollbars');
            }

            //IE doesn't allow focus, so I close it and open a new one
            if (navigator.appName == 'Microsoft Internet Explorer') {
                winRef.close();
                winRef = window.open(url, 'width=800,height=600,resizable,scrollbars');
            }
            else {
                //give it focus for a better user experience
                winRef.focus();
            }
        }
        list.add(name);
        win_refs.add(winRef);
        win_refs_items = win_refs.items();
        var length = win_refs_items.length;
        for(var count=0;count<length;count++){
        var timer = setInterval(function() {   
        if(win_refs_items[count].closed) {  
            clearInterval(timer);  
            alert("closed");
            list.remove(name);  
        }  
        }, 1000);
        }
}

我用于cookie处理的函数(add /删除/清除/项目): http://pastebin.com/raw.php?i=647fGSJv

The functions which I used for cookie handling (add/remove/clear/items) are: http://pastebin.com/raw.php?i=647fGSJv

推荐答案

正如您已经想到的那样,变量在运行进程之外不存在。因此,你不能将它们保存在例如cookies。

As you've already figured out, variables do not exist outside running processes. Thus you can't save them in e.g. cookies.

显而易见的方法是跟踪窗口名称:

The obvious approach would be to keep track of window names:


var windowObjectReference = window.open(strUrl, strWindowName [,strWindowFeatures]);

var windowObjectReference = window.open(strUrl, strWindowName[, strWindowFeatures]);

这些是字符串所以可以存储在任何地方。但是,某些浏览器(即Chrome)在独立线程中打开标签,这意味着一旦关闭父窗口,子窗口就会无法访问。

Those are strings so can be stored anywhere. However, some browsers (namely Chrome) open tabs in independent threads, which means that once you close the parent window, child ones become unreachable.

我想你可以试试反之亦然:

I think you could try to do it the other way round:


  • 为每个子窗口分配唯一ID

  • 商店ID (本地存储,cookie,等等)

  • 按ID分配每个子窗口轮询,直到他们收到工作为止

这篇关于如果父刷新,则检索对子窗口的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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