|
||||||||||||||||||||||||
|
js detection of window.closed condition
- The problem being that window.unload fires not only when the window closes but also when the document therein is reloaded (like when going to a different url).
- The problem also being that the win.closed property should optimally be tested by event and not poll driven code, the most relevant event being the window.unload of the window in question.
- The problem also being that the window itself cannot test its own win.closed as it will remain false as long as there is an active js call stack (code being executed) in it.
Attention: this solution is IE only. A cross-browser solution is left as an exercise to the reader
Click to see it work:
<script language="JScript">
var win;
function setup(){
win = window.open();
//You can execute the script on the window from the opener thus
win.execScript("window.onunload = function(){opener.detectWin();}", "JScript");
//or have the script embedded in the page that the window.open() call points to
}
function detectWin(){
//setTimeout is required, because while the call stack of the window
//is not exhausted the window is still considered open
setTimeout("alert('this will be true because the setTimeout() call is " +
"executed on a new stack " +
"and therefore the child window call stack gets exhausted, " +
"the window is subsequently closed and this fact is verified " +
"by this: win.closed = ' + win.closed);", 0);
alert("This returns false because the child window js call stack is not " +
"exhausted and it has to before it can close (check your taskbar and " +
"see that the window is still there) win.closed = " + win.closed);
}
</script>




Post new comment