如何為 網頁瀏覽器控制主機處理指令碼錯誤

來源:互聯網
上載者:User

http://support.microsoft.com/kb/261003

當其中一個指令碼引擎遇到未處理的錯誤時,它將轉寄到 網頁瀏覽器控制項,然後查詢以查看容器已經實現了宿主其容器的錯誤。如果容器已實現宿主,web
瀏覽器控制項調用CGID_DocHostCommandHandler (這在 Mshtmhst.h 檔案中定義) 的命令組 ID 和OLECMDID_SHOWSCRIPTERROR的命令 ID 的IOleCommandTarget::Exec方法。如果主機不會返回S_OK,MSHTML 顯示預設的"此頁上出現錯誤"錯誤資訊。

下面的代碼示範如何?文件物件模型中檢索資訊時此命令 id 的處理常式。此代碼不會說明了錯誤處理。

: 如果使用者已清除在 Internet 選項 下的 進階 選項卡中的 禁用指令碼調試 複選框,將不會調用此方法。

   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);   }

如何重寫的 MFC 控制項容器介面方法看這裡:http://blog.csdn.net/mail_cm/article/details/7817624

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.