PRB: showModalDialog and showModelessDialog Do Not Fire NewWindow2

This article was previously published under Q251128
This article has been archived. It is offered "as is" and will no longer be updated.
Calling showModalDialog or showModelessDialog does not fire the NewWindow2 event. This applies to DWebBrowserEvents2::NewWindow2 as well as its Microsoft Visual Basic equivalent, the NewWindow2 event for the WebBrowser and InternetExplorer objects.
The NewWindow2 event is only fired when a new instance of Internet Explorer is about to be created. The modal/modeless dialogs are not new instances of Internet Explorer. They are implemented as MSHTML host windows, allowing them to render and display HTML content (but not hyperlinks between documents). Furthermore, they do not appear in the taskbar and are closed whenever the window that created them is closed.
This behavior is by design.
More information

Steps to Reproduce Behavior

  1. Copy the following text into a new HTML file called NoNewWnd.htm:
    <HTML><HEAD><TITLE>NewWindow2 Test</TITLE><SCRIPT LANGUAGE="JScript">function openNewWindow(szMethod) {   if (szMethod == "modal") {      window.showModalDialog("");   }   else if (szMethod == "modeless") {      window.showModelessDialog("");   }   else {"");   }   }</SCRIPT></HEAD><BODY><P><INPUT type="button" value="" id=button1 name=button1 onclick="openNewWindow('normal');"><BR><INPUT type="button" value="showModalDialog" id=button2 name=button2 onclick="openNewWindow('modal');"><BR><INPUT type="button" value="showModelessDialog" id=button3 name=button3 onclick="openNewWindow('modeless');"></P></BODY></HTML>					
  2. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  3. Add a reference to the WebBrowser control by selecting Components on the Project menu and selecting Microsoft Internet Controls (Shdocvw.dll).

    The WebBrowser control will appear in the Toolbox.
  4. Create a new instance of the WebBrowser control on Form1.
  5. Copy the following code into the code window of Form1.
    Private Sub Form_Load()  ' Replace "C:\noNewWnd.htm" with the absolute path  ' to the HTML file you created in Step 1.  strFileName = "C:\noNewWnd.htm"  WebBrowser1.Navigate2 strFileNameEnd SubPrivate Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)  MsgBox "I just caught NewWindow2!"End Sub					
  6. From the Run menu, click Start or press the F5 key to start the program. Click on each of the three buttons and observe the behavior.

    NOTE: The showModalDialog method was not available prior to Internet Explorer 5 and will cause a run-time error on those versions.
For additional information, please see the following MSDN Web Workshop Web sites:
IHTMLWindow2::showModalDialog, IHTMLWindow3::showModelessDialog

Article ID: 251128 - Last Review: 11/02/2013 22:37:00 - Revision: 4.0

Microsoft Internet Explorer 4.01 Service Pack 1

  • kbnosurvey kbarchive kbdhtml kbieobj kbnofix kbprb KB251128