请教一下,如何屏蔽脚本错误.我是自己做的浏览器,用CHtmlView类做的,打开新页面用OnNewWindow2方法有些网站打开时都会报IE的脚本错误.我想请教一下,如何在我的浏览器屏蔽错误提示.
下面是IE屏蔽错误提示的方法:
Internet 选项==>高级 ==>禁用脚本调试-->打上勾即可以下是一段英文资料,看不懂....sigh~~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);
}

解决方案 »

  1.   

    写个脚本, onerror=xxx 的 JS 文件,让在控件里运行起来
      

  2.   

    这个方法我查到
    http://www.cnblogs.com/zhangqingping/archive/2009/06/16/1504260.html
    WebBrowser或CHtmlView中轻松屏蔽脚本错误(JavaScript)不知道有什么副作用,还是想用MSDN上推荐的COM接口方法
      

  3.   

    有人会COM技术么?还有人做过呀
      

  4.   

    碰到和你一样的问题.在网上找了很久,发现有不彻底的,有很复杂的.
    我还是用定时器在OnTimer(UINT nIDEvent) 里处理的方法来暂代一下.
    HWND hWnd;
    hWnd=::FindWindow("#32770","错误"); 
    if (hWnd)
    {
    HWND hCurrentWindow;
    char ll[256]={0};
    hCurrentWindow=::GetWindow(hWnd,GW_CHILD);
    while  (hCurrentWindow)
    {
    if  (::GetWindowText(hCurrentWindow,ll,255)) 
    {
    if (!strncmp(ll,"否(&N)",6)) //"否(&N)"
    {
    CPoint point;
    GetCursorPos(&point);
    CRect rt;
    ::GetWindowRect(hCurrentWindow,rt); 

    SetCursorPos(rt.left+20,rt.top+10);//  
    mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); 
    Sleep(10);
    mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); 

    SetCursorPos(point.x,point.y);


    break;
    }
    }
    hCurrentWindow=::GetWindow(hCurrentWindow,GW_HWNDNEXT);
    }
    }

    hWnd=::FindWindow("Internet Explorer_TridentDlgFrame","Internet Explorer 脚本错误"); 
    // CRect rc;
    // HWND   hWnd=::FindWindow("IEFrame","迅援在线交流平台 - Windows Internet Explorer"); 
    if (hWnd)
    ::SendMessage(hWnd, WM_CLOSE, 0, 0);//