Reputation: 11734
I have code to check if a window is closed. It works IF I stay on the same page.
With Internet Explorer, if I click on a link that then redirects to another site, that window.closed returns true even though the WINDOW never actually is closed.
I am doing this:
w = window.open("mypage.html");
var t = setInterval(function() {
if (!w) { alert('a'); }
if (w.closed) { alert('b'); }
if (!w || w.closed) {
clearInterval(t); hide('mainShadow');
}
}, 800);
Within "mypage.html", there is a link to another site. When going to that other side, the w.closed returns true
.
Is there a good way in IE to really check if the window is closed or not.
The contract isn't really satisfied because the window never actually closed.
This code works in Chrome, not in IE9
Upvotes: 6
Views: 1377
Reputation: 7430
The contract isn't really satisfied because the window never actually closed.
This seems to be the origin of the confusion. The window
object in JavaScript-in-a-browser refers to window displaying a document as embodied in some browser window, but it does not refer to that window as the browser sees it itself. Nor is it the case that a single browser window is associated with a single window
instance. So a window
instance can be closed because it's not longer displayed. That's accurate because the window
element is not, strictly speaking, about the browser entity window
that is an external view of the window, as opposed the JavaScript object window
that is an internal view of the window that a document containing code sees.
The short version is that I don't believe there's a way to do this from within a web page, since the outer scope of visibility, the window
object, doesn't refer to the actual object you want to manipulate.
Upvotes: 2
Reputation: 6680
There is no way you can prevent this functionality due to the same-origin policy. However, you might be able to use window.opener.someFunction() to call a function in the parent window every 100 ms. If the parent window doesn't receive a message after say 500 ms, then it can assume that the window it opened was closed. You could try similar with window.postMessage, but it won't work in IE.
Upvotes: 0