【问题标题】:Retrieving references to child window if the parent refreshes如果父窗口刷新,则检索对子窗口的引用
【发布时间】:2014-08-08 17:22:21
【问题描述】:

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

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

要在子窗口关闭时发出警告,我在 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 被重新初始化。

为了解决这个问题,我使用了 cookie,但它不起作用。我在下面的代码中遗漏了什么/做错了什么?

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 处理(添加/删除/清除/项目)的函数是:http://pastebin.com/raw.php?i=647fGSJv

【问题讨论】:

    标签: javascript jquery jquery-cookie


    【解决方案1】:

    正如您已经发现的那样,变量不存在于正在运行的进程之外。因此,您无法将它们保存在例如饼干。

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

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

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

    我认为你可以尝试反过来做:

    • 为每个子窗口分配一个唯一的 ID
    • 存储 ID(本地存储、cookie 等)
    • 让每个子窗口按 ID 进行轮询,直到他们收到工作为止

    【讨论】:

    • 从子窗口轮询到服务器,然后根据它做出决定听起来是个好主意,虽然我认为在我的情况下这会有点过分,无论如何你的回答涵盖了我问题的所有要点,谢谢!
    • 顺便说一句,如果我在 cookie 中有子窗口名称,那么我可以检查该窗口是否已打开? (即使父级刷新)
    • @ChankeyPathak - 评论 #1:新的父窗口可以在公共存储(本地存储、cookies...)中写入内容,子窗口可以在那里检查; AJAX 并不是必须的。评论 #2:我认为您可以在大多数浏览器中使用,但 Chrome 肯定会拒绝提供帮助。和
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多