How to handle script errors as a WebBrowser control host

Article translations Article translations
Article ID: 261003 - View products that this article applies to.
This article was previously published under Q261003
Expand all | Collapse all

SUMMARY

The WebBrowser control notifies its host of an unhandled script error through the IOleCommandTarget interface. The host can then retrieve the information about the error, display (or suppress) an error message to the user, and choose whether to run scripts on the page.

MORE INFORMATION

When one of the script engines encounters an unhandled error, it forwards the error to the WebBrowser control, which then queries its container to see if the container has implemented IOleCommandTarget. If the container has implemented IOleCommandTarget, the WebBrowser control calls the IOleCommandTarget::Exec method with the command group ID of CGID_DocHostCommandHandler (which is defined in the Mshtmhst.h file) and a command ID of OLECMDID_SHOWSCRIPTERROR. If the host does not return S_OK, MSHTML displays the default "An error has occurred on this page" error message.

The following code illustrates how to implement a handler for this command ID that retrieves the error information from the document object model. This code does not illustrate error handling.

NOTE: This method will not be invoked if the user has cleared the Disable Script Debugging check box in the Advanced tab under Internet Options.
   STDMETHODIMP CMyBrowser::Exec( const GUID* pguidCmdGroup, DWORD nCmdID,
      DWORD nCmdexecopt, VARIANTARG* pvaIn, VARIANTARG* pvaOut )
   {

      HRESULT hr = S_OK;

      if (pguidCmdGroup && IsEqualGUID(*pguidCmdGroup, CGID_DocHostCommandHandler))
      {
         
         switch (nCmdID) 
         {
         
         case OLECMDID_SHOWSCRIPTERROR:
         {
            IHTMLDocument2*             pDoc = NULL;
            IHTMLWindow2*               pWindow = NULL;
            IHTMLEventObj*              pEventObj = NULL;
            BSTR                        rgwszNames[5] = 
                                        { 
                                           SysAllocString(L"errorLine"),
                                           SysAllocString(L"errorCharacter"),
                                           SysAllocString(L"errorCode"),
                                           SysAllocString(L"errorMessage"),
                                           SysAllocString(L"errorUrl")
                                        };
            DISPID                      rgDispIDs[5];
            VARIANT                     rgvaEventInfo[5];
            DISPPARAMS                  params;
            BOOL                        fContinueRunningScripts = true;
            int	                        i;

            params.cArgs = 0;
            params.cNamedArgs = 0;

            // Get the document that is currently being viewed.
            hr = pvaIn->punkVal->QueryInterface(IID_IHTMLDocument2, (void **) &pDoc);				
            // Get document.parentWindow.
            hr = pDoc->get_parentWindow(&pWindow);
            pDoc->Release();
            // Get the window.event object.
            hr = pWindow->get_event(&pEventObj);
            // Get the error info from the window.event object.
            for (i = 0; i < 5; i++) 
            {  
               // Get the property's dispID.
               hr = pEventObj->GetIDsOfNames(IID_NULL, &rgwszNames[i], 1, 
                       LOCALE_SYSTEM_DEFAULT, &rgDispIDs[i]);
               // Get the value of the property.
               hr = pEventObj->Invoke(rgDispIDs[i], IID_NULL,
LOCALE_SYSTEM_DEFAULT,
                       DISPATCH_PROPERTYGET, &params, &rgvaEventInfo[i],
NULL, NULL);
               SysFreeString(rgwszNames[i]);
            }

            // At this point, you would normally alert the user with 
            // the information about the error, which is now contained
            // in rgvaEventInfo[]. Or, you could just exit silently.

            (*pvaOut).vt = VT_BOOL;
            if (fContinueRunningScripts)
            {
               // Continue running scripts on the page.
               (*pvaOut).boolVal = VARIANT_TRUE;
            }
            else
            {
               // Stop running scripts on the page.
               (*pvaOut).boolVal = VARIANT_FALSE;			
            } 
            break;
         }
         default:
            hr = OLECMDERR_E_NOTSUPPORTED;
            break;
         }
      }
      else
      {
         hr = OLECMDERR_E_UNKNOWNGROUP;
      }
      return (hr);
   }
				

REFERENCES

For additional information on implementing the IOleCommandTarget interface, please see the following Microsoft Developer Network (MSDN) Web site:
http://msdn.microsoft.com/library/default.asp?URL=/library/psdk/com/oin_oc_9bg4.htm
For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:
http://msdn.microsoft.com/ie/

http://support.microsoft.com/iep

Properties

Article ID: 261003 - Last Review: April 21, 2006 - Revision: 3.0
APPLIES TO
  • Microsoft Internet Explorer 5.0
  • Microsoft Internet Explorer 5.01
  • Microsoft Internet Explorer 5.5
Keywords: 
kbhowto kbwebbrowser KB261003
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com