Been busying on investigating Dynamics CRM 4 Web Application and Outlook VSTO Add-In development for the past weeks and not update blog much. It's time to do a post now.

Little but useful trick when toying with IE windows.

When there is only a parent IE window and you load some pages with javascript "window.close();" that trying to close the window, in IE there will be a confirming dialog box asking if one really want to close the IE window. This often causes problems if you want to host your IE in a winform, and here are some simple and quick workaround to overcome this.

   1: <HTML>
   2: <HEAD>
   3: <TITLE>Close window without prompt</TITLE>
   4: <SCRIPT LANGUAGE="JavaScript">
   5: <!--
   6: function realClose()
   7: {
   8:     var win=window.open("","_top","","true");
   9:     win.opener=true;
  10:     win.close();
  11: }
  12: //-->
  13: </SCRIPT>
  14: </HEAD>
  15: <BODY>
  16: Close window without prompt
  17: <FORM><INPUT TYPE="button" VALUE="Close ME!" onClick="realClose()"></FORM>
  18: </BODY>
  19: </HTML>

The trick is to open an empty page in self window (the "_top"), and then close the opened window in javascript to fool IE that he is closing a child window. this worked without problems.

Further more, if you don't have ways to modify the page you are opening, thus not able to embed the realClose() function to the page, maybe in a WinForm that hosting a IE window and actually you don't know what page you are going to open, maybe this way would work.

   1: <HTML>
   2: <HEAD>
   3: <TITLE>Close window without prompt</TITLE>
   4: <SCRIPT LANGUAGE="JavaScript">
   5: <!--
   6: function realClose()
   7: {
   8:     var win=window.open("","_top","","true");
   9:     win.opener=true;
  10:     //win.close();
  11:     win.realclosefunc();
  12: }
  13: window.realclosefunc = window.close;
  14: window.close = realClose;
  15: //-->
  16: </SCRIPT>
  17: </HEAD>
  18: <BODY>
  19: Close window without prompt
  20: <FORM><INPUT TYPE="button" VALUE="Close ME!" onClick="javascript: window.close();"></FORM>
  21: </BODY>
  22: </HTML>
  23:  

in this way, when in WinForm, try to manipulate DOM of the hosting IE window, and try to insert the piece of code in the head part of page, then change the default window.close function to our self-made function to close the window. using this way won't need to do a string replacement of the downloaded html contents to replace window.close() to our close function. little bit tidy, but still doable...

For WinForm hosting pattern, using .net 2.0 WinForm WebBrowser control won't give you that magic WindowClosing event to prevent IE closing in your WinForm. There are articles introducing hooking DWebBrowserEvents2 interface to get this event working with WebBrowser control, but for no reason it just not working.

So I just tried to use COM Interop way to bridge the ActiveX IE control to .net by using aximp.exe command, and by doing this found that the WindowClosing event is working and able to detect the IE close before that confirming dialog box showed up, and then to cancel the IE close action inside the event and close my WinForm. this should be the easiest way to accomplish this task I think.

FYI.