Window.closed property

JavaScript FAQ | JavaScript Windows FAQ  

Question: How do I test whether my other window is closed or still open?

Answer: Let's assume that you opened a new browser window using the

window.open()
method
winRef = window.open( URL, name, features )
Later on, you can check whether this window is still open by using the
window.closed
property:
if (winRef.closed) alert ("It's closed!")
else alert ("It's still open!")
One problem with the
closed
property is that the
winRef
window reference itself may be undefined if the
winRef
window is not currently open and has never been opened before. This situation may cause script errors. To avoid this kind of errors, your window-opener webpage can initialize
winRef
and
winRef.closed
early on, before all other script code, which would ensure that
winRef.closed
is defined at all times. You can accomplish this by including these statements in the very beginning of your first script block:
winRef = new Object();
winRef.closed = true;
Try this technique using the test below:

See also:
How do I open a new browser window?
How do I close a window?
How do I write script-generated content to another window?

Copyright © 1996-2018, JavascriptFAQ.net.