BUG: Window.Closed Property Fails in WebBrowser Control

ID: Q244375


The information in this article applies to:
  • Microsoft Internet Explorer (Programming) versions 4.0, 4.01, 4.01 SP1, 4.01 SP2, 5


SYMPTOMS

A Web page hosted in a WebBrowser control may encounter the following runtime error when attempting to use the window.closed property:

The remote server machine does not exist or is unavailable
This error occurs only when there is no other instance of Internet Explorer running. It works fine if an instance of Internet Explorer is launched, before running the application that hosts the WebBrowser control.


RESOLUTION

If the WebBrowser control application handles the NewWindow2 event to create new browser windows, no error occurs, but the window.closed property always returns false.

There is no way to return a valid result with the window.closed property in WebBrowser applications that manage new windows via NewWindow2. The best resolution is to alter the Web page script on the closing window to notify the opener window when it is closing. This can be done in the onUnload event of the window object. The opener window can be reached through the opener property. Here is a JavaScript example:


<SCRIPT FOR=window EVENT=onUnload>
// variable WindowClosed has been defined at global scope in the
// opening page. This page will now check WindowClosed instead of
// "window.closed" to see if the opened window is still around
window.opener.WindowClosed = true;
</SCRIPT>
<SCRIPT FOR=window EVENT=onLoad>
window.opener.WindowClosed = false;
</SCRIPT> 
WebBrowser applications that do not handle the NewWindow2 event can catch the runtime error in client script using javascript exception handling or "On Error Resume Next" in VBScript. Here is a JavaScript example:

   try
   {
      bClosed = otherwin.closed;
   }
   catch(e)
   {
      alert("The Other Window is Currently Closed (exception thrown)");
      return;
   }
   if (bClosed)
   {
       alert("The Other Window is Currently Closed");
   }
   else
   {
       alert("The Other Window is Currently Open");
   } 
NOTE: Javascript exception handling requires version 5.0 of the JScript engine, which ships with Internet Explorer 5.


STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.


REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

Q184876 HOWTO: Use the WebBrowser Control NewWindow2 Event
For more information, please see the MSDN Web Workshop:
http://msdn.microsoft.com/workshop/default.asp

Additional query words:

Keywords : kbDHTML kbIE400bug kbIE401bug kbWebBrowser kbIE401sp1bug kbIE401sp2bug kbGrpInet kbIE500bug kbDSupport
Version : WINDOWS:4.0,4.01,4.01 SP1,4.01 SP2,5
Platform : WINDOWS
Issue type : kbbug


Last Reviewed: November 20, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.