FIX: Canceling BeforeNavigate Shows Dialog with 'X'

ID: Q176116

The information in this article applies to:

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 
Version           : WINDOWS:1.0,3.0,3.01,3.02
Platform          : WINDOWS
Issue type        : kbbug
Solution Type     : kbfix

Last Reviewed: February 7, 1998