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.
3: <TITLE>Close window without prompt</TITLE>
6: function realClose()
8: var win=window.open("","_top","","true");
16: Close window without prompt
17: <FORM><INPUT TYPE="button" VALUE="Close ME!" onClick="realClose()"></FORM>
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.
13: window.realclosefunc = window.close;
14: window.close = realClose;
19: Close window without prompt
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.