简介摘要:一、屏蔽脚本错误提示 (转)c# webbrowser 加载网页出错解决方法 2009-03-25 19:42 当IE浏览器遇到脚本错误时浏览器,左下角会出现一个黄色图标,点击可以查看脚本错误的详细信息,并不会有弹出的错误信息框。当我们使用WebBrowser控件时有错误信

 

一、屏蔽[ping bi]脚本[jiao ben]错误[cuo wu]提示[ti shi]

 (转)c# webbrowser 加载网页[wang ye]出错解决方法[fang fa]   2009-03-25 19:42           当IE浏览[liu lan]器[liu lan qi]遇到脚本[jiao ben]错误[cuo wu]时浏览[liu lan]器[liu lan qi],左下角会出现一个黄色图标[tu biao],点击可以查看脚本[jiao ben]错误[cuo wu]的详细信息[xin xi],并不会有弹出的错误[cuo wu]信息[xin xi]框。当我们使用WebBrowser控件时有错误[cuo wu]信息[xin xi]框弹出,这样程序显的很不友好,而且会让一些自动执行[zhi hang]的程序暂停[zan ting][cheng xu zan ting]。我看到有人采取的解决方案[jie jue fang an]是做一个窗体[chuang ti]杀手程序来关闭弹出的窗体[chuang ti]。今天探讨的方法[fang fa]是从控件解决问题[wen ti]。


 1、SHDocVw.dll 

在COM时代我们使用的WebBrowser控件是SHDocVw.dll。屏蔽[ping bi]错误[cuo wu]信息[xin xi]的方法[fang fa]很简单使用下面的一句就可以搞定。

WebBrowser1.Silent =  true ;

 2、.Net中 

在.Net中提供了托管的WebBrowser可供我们使用,当然我们仍然可以在.Net中使用COM组建SHDocVw.dll,如果使用SHDocVw.dll
处理错误[cuo wu]方式和上面的方法[fang fa]一样。但如果我们是使用.Net组件[zu jian]如何解决这个问题[wen ti]呢?

这个组件[zu jian]给我们提供了一个方法[fang fa]ScriptErrorsSuppressed 。但是在.net framework2.0中他是不起作用[zuo yong]的,据说在低版本中使用如下的方式解决

webBrowser1.ScriptErrorsSuppressed =  true ;

(据说在.net framework2.0以前是这样,我没有使用过)

那么在.net framework2.0中如何解决这个问题[wen ti]呢?

有一种方法[fang fa]不能彻底解决,可以部分解[fen jie]决问题[wen ti]这里也介绍给大家。
//捕获[bu huo]控件的错误[cuo wu]
this.WebBrowser.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
//对错误[cuo wu]进行处理
void Window_Error(object sender, HtmlElementErrorEventArgs e)
{
     // 自己的处理代码[dai ma]
    e.Handled = true;
}

 3、上面的方法[fang fa]对于多个框架[kuang jia]嵌套[qian tao]等等的情形还是不能很好的解决。 

为了彻底解决这个问题[wen ti],我们借助AxWebBrowser来解决WebBrowser的问题[wen ti]。

我们定义一个自己的类,他的父类[fu lei]是WebBrowser,以后使用这个类就可以了。在这个类的定义中需要引用[yin yong]SHDocVw。
class EWebBrowser : System.Windows.Forms.WebBrowser
{
    SHDocVw.IWebBrowser2 Iwb2;

    protected override void AttachInterfaces(object nativeActiveXObject)
    {
        Iwb2 = (SHDocVw.IWebBrowser2) nativeActiveXObject;
        Iwb2.Silent = true;
        base.AttachInterfaces(nativeActiveXObject);
    }

    protected override void DetachInterfaces()
    {
        Iwb2 = null;
        base.DetachInterfaces();
    }
}

//项目[xiang mu]中添加Micrsoft.mshtml引用[yin yong]
using mshtml;

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
    vDocument.parentWindow.execScript(
        "function alert(str){if(str=='zswang')confirm(str);}", "javaScript");
}

//frame结构[jie gou]

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
    foreach (IHTMLElement vElement in vDocument.all)
        if (vElement.tagName.ToUpper() == "FRAME")
        {
            IHTMLFrameBase2 vFrameBase2 = vElement as IHTMLFrameBase2;
            vFrameBase2.contentWindow.execScript(
                "function alert(str){confirm('[' + str + ']');}", "javaScript");
        }
}
二、屏蔽[ping bi]其它窗口[chuang kou]
            (wb.ActiveXInstance as SHDocVw.WebBrowser).NavigateComplete2 += new DWebBrowserEvents2_NavigateComplete2EventHandler(wb_NavigateComplete2);//wb是一个Webbrowser控件

        //屏蔽[ping bi]一些弹出窗口[chuang kou]
        void wb_NavigateComplete2(object pDisp, ref object URL)
        {
            mshtml.IHTMLDocument2 doc = (wb.ActiveXInstance as SHDocVw.WebBrowser).Document as mshtml.IHTMLDocument2;
            doc.parentWindow.execScript("window.alert=null", "javascript");
            doc.parentWindow.execScript("window.confirm=null", "javascript");
            doc.parentWindow.execScript("window.open=null", "javascript");
            doc.parentWindow.execScript("window.showModalDialog=null", "javascript");
            doc.parentWindow.execScript("window.close=null", "javascript");

三、自动确定弹出对话框[dui hua kuang]

Q:winform中如何实现自动点击webbrowser弹出对话框[dui hua kuang]中的确定按钮

A:

//using mshtml;
        //using SHDocVw;
        private void Form1_Load(object sender, EventArgs e)
        ...{
            this.webBrowser1.Navigate("  http://localhost:28512/WebSite2/Default.aspx  ");
            SHDocVw.WebBrowser wb = this.webBrowser1.ActiveXInstance as SHDocVw.WebBrowser;
            wb.NavigateComplete2 += new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(wb_NavigateComplete2);
            
        }

        void wb_NavigateComplete2(object pDisp, ref object URL)
        ...{
            mshtml.IHTMLDocument2 doc = (this.webBrowser1.ActiveXInstance as SHDocVw.WebBrowser).Document as mshtml.IHTMLDocument2;
            doc.parentWindow.execScript("function alert(str){return ''}", "javascript");
        }

posted on 2010-08-26 16:38  freedom831215  阅读(1205)  评论(0编辑  收藏  举报