FIX: Canceling BeforeNavigate Shows Dialog with 'X'

Last reviewed: February 6, 1998
Article ID: Q176116
The information in this article applies to:
  • Microsoft Internet Explorer (Programming), versions 3.0, 3.01, 3.02
  • Microsoft Internet ActiveX Controls, version 1.0
  • Microsoft ActiveX SDK, version 1.0

SYMPTOMS

The Internet Explorer WebBrowser control has an event called BeforeNavigate(). BeforeNavigate() is called with a BOOLEAN pointer (Cancel) that you can set to TRUE to prevent the navigation from occurring. In C++, if you set the Cancel parameter to TRUE, the navigation will be cancelled as it should, but you will also see a blank dialog box with an "X." In Visual Basic, you will get a runtime error 287 (Application-defined or object-defined error).

CAUSE

The WebBrowser control is incorrectly throwing an exception with an error code of zero.

RESOLUTION

This bug is fixed in Internet Explorer 4.0.

For people using Internet Explorer version 3.02 and earlier, you can work around this problem by wrapping the call to Navigate() with an exception handler, and ignore an error if the error code is zero. Visual Basic sets the error number to 287 (Application-defined or object-defined error) if the error code was originally zero, so you should check for this instead.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug has been corrected in Internet Explorer 4.0.

MORE INFORMATION

   ********** Start of sample code in Visual C++ (MFC) **********
      // OnClickNavigate is a button-click handler
      void CMyDialog::OnClickNavigate()
      {
         try
         {
            COleVariant var;
            m_browser.Navigate ("http://www.microsoft.com", var, var, var,
               var);
         }
         catch (COleDispatchException* pExcept)
         {
            // if the error code is nonzero we want the error to occur
            if (pExcept->m_wCode)
            {
               pExcept->Delete ();
               throw;
            }
            // if the error code is zero then we just eat it
            else
               pExcept->Delete ();
         }
      }

      void CMyDialog::BeforeNavigate(LPCTSTR URL, long Flags, LPCTSTR
   TargetFrameName, VARIANT FAR* PostData,
   LPCTSTR Headers, BOOL FAR* Cancel)
      {
         // cancel navigation if user clicks on "no"
         if (IDNO == MessageBox ("Confirm Navigate", "", MB_YESNO))
            *Cancel = TRUE;
      }
   ********** End of sample code in Visual C++ (MFC) **********

   ********** Start of sample code in Visual Basic **********
      Private Sub cmdNavigate_Click()
         ' set error handler
         On Error GoTo ErrorHandler
         WebBrowser1.Navigate "http://www.microsoft.com"
         ' reset error handler to default
         On Error GoTo 0
         Exit Sub
      ErrorHandler:
         ' if error code is not 287, let error occur
         If Err <> 287 Then Err.Raise Err
         On Error GoTo 0
      End Sub

    Private Sub WebBrowser1_BeforeNavigate(ByVal URL As String, ByVal Flags
     As Long, ByVal TargetFrameName As String, PostData As Variant,
     ByVal Headers As String, Cancel  As Boolean)
          ' cancel navigation if user clicks "no"
         If vbNo = MsgBox("Confirm Navigate", vbYesNo) Then
            Cancel = True
         End If
      End Sub
   ********** End of sample code in Visual Basic **********

REFERENCES

ActiveX SDK online documentation


Additional query words: web browser
Keywords : AXSDKWebBrowser kbtlc
Technology : kbInetDev kbole
Version : WINDOWS:1.0,3.0,3.01,3.02
Platform : WINDOWS
Issue type : kbbug
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 6, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.