WinForm開發(fā)中屏蔽WebBrowser腳本錯誤提示的方法

字號:


    通常在C#的實際開發(fā)過程中,會發(fā)現(xiàn)設(shè)置其屬性ScriptErrorsSuppressed無法達(dá)到屏蔽腳本錯誤效果,但是可以通過下面兩種方式實現(xiàn)這一效果。
    1.在DocumentCompleted事件中訂閱Error處理,代碼如下所示:
    private void wbGoogleMap_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
    wbGoogleMap.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
    }
    void Window_Error(object sender, HtmlElementErrorEventArgs e)
    {
    e.Handled = true;
    }
    2.在腳本中window.onerror中處理,代碼如下所示:
    window.onerror = function(error, url, line) {
    // log(error + "url:" + url + "lineNo:" + line);
    return true;
    }
    通過上述兩種方法能夠很好的屏蔽WebBrowser腳本錯誤提示。希望本文所述方法對大家的C#程序設(shè)計有所幫助!