Retrieving references to child window if the parent refreshes

╄→гoц情女王★ 提交于 2020-01-05 03:00:29

问题


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

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

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.

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

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


回答1:


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

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:

  • Assign a unique ID to each child window
  • Store IDs (local storage, cookies, whatever)
  • Make each child window poll by ID until they receive a job for them


来源:https://stackoverflow.com/questions/25177076/retrieving-references-to-child-window-if-the-parent-refreshes

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