Check If Popup Window Is Already Open
How can I modify this so it checks and if a popup window is already open? function popUp(URL) { day = new Date(); id = day.getTime(); eval('page' + id + ' = window.open(URL,
Solution 1:
Use the same id
for the window, which is the name of the window, to reuse it if it exists.
http://www.w3schools.com/jsref/met_win_open.asp
var options = "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=350,height=100";
var popup = window.open(URL, "popup", options);
To do this with an anchor tag you just set the target
If you set an anchor tag's target target="myopup"
it will use that same window if it exists
Solution 2:
My answer is to this question linked to here.
Check the following code.
You can use onbeforeunload to get a callback before the window unloads.
To check if this is due to window.close
do this
setTimeout(function(){
if(anc.closed) console.log('closed');
elseconsole.log('refreshed');
},1000); // to check after the window closed
FULL CODE
var anc = window.open('https://stackoverflow.com');
var anc.onbeforeunload = function() { // called before closing the window.setTimeout(function() {
if (anc.closed) console.log('closed');
elseconsole.log('refreshed');
}, 1000); // to check after the window closed
}
Post a Comment for "Check If Popup Window Is Already Open"