通常在C#的實際開發過程中,會發現設置其屬性ScriptErrorsSuppressed無法達到屏蔽腳本錯誤效果,但是可以通過下面兩種方式實現這一效果。
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#程序設計有所幫助!
新聞熱點
疑難解答