【发布时间】: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