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