6

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

2
  • You are aware of you are clearing a timeout not an interval? You should use clearInterval. But anyway good question. Commented Nov 15, 2012 at 21:27
  • Apparently clearTimeout also works. I don't know if internally one calls the other. Commented Nov 15, 2012 at 21:31

2 Answers 2

2
+150

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

I going to some third-party site. Think google.com. I don't have access to the functionality of the sites in the popup. Also, Chrome seems to have the functionality I need including when I move to some third-party site (say google). Internet Explorer 9 does not.
Then you can't do anything about Internet Explorer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.