Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if window.closed but also with a redirect to another URL/Host

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

like image 438
Berlin Brown Avatar asked Nov 15 '12 21:11

Berlin Brown


1 Answers

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.

like image 135
eh9 Avatar answered Sep 23 '22 19:09

eh9