What should I do if a script error occurs when I use webBrowser to open a webpage?
When the IE browser encounters a script error, a yellow icon appears in the lower-left corner of the browser. You can click it to view the detailed information about the script error. No error message box is displayed. When we use a webBrowser program to open the webpage, an error prompt box will pop up, and the script can be executed only after confirmation. If the program we designed is used to automatically process web pages, in this case, the program is interrupted and manual intervention is required. This obviously cannot meet our requirements.
So how can I handle a script error when I use webBrowser to open a webpage so that the program runs automatically without interference?
WebBrowser provides a property: ScriptErrorsSuppressed. If you do not want to encounter a script error, a prompt box is displayed. You can set this value to TRUE.
WebBrowser1.ScriptErrorsSuppressed = true;
The specific usage of the ScriptErrorsSuppressed attribute is as follows:
Set this property to false to debug webpages displayed in the WebBrowser control. This attribute is useful if you want to use this control to add Web-based controls and script code to an application. This property is of little use if you use this control as a generic Browser. After debugging the application, set this property to true to cancel the display of script errors. Http://hovertree.com/h/bjaf/scjyuanma.htm
Note: When ScriptErrorsSuppressed is set to true, the WebBrowser control hides all the dialogs from the Basic ActiveX control, not just script errors. Sometimes, you may need to cancel displaying script errors when displaying certain dialogs (for example, for browser security settings and user logon. In this case, set ScriptErrorsSuppressed to false and cancel displaying script errors in the handler of the HtmlWindow. Error Event.
The negative effect of this method is described in the red letter above. If you only want to block script errors, you can use the following method:
The following code demonstrates how to cancel displaying a script error Without undisplaying any other dialog box. In this example, set the ScriptErrorsSuppressed attribute to false to ensure that the dialog box is displayed. The Error is displayed when the handler of the HtmlWindow. Error event is canceled. This event can be accessed only when the document has been loaded. Therefore, this handler is appended to the DocumentCompleted event handler.
// Only hide the Script Error. For other errors, the private void SuppressScriptErrorsOnly (WebBrowser browser) prompt is displayed. {// make sure that ScriptErrorsSuppressed is set to false. browser. scriptErrorsSuppressed = false; // process the DocumentCompleted event to access the Document Object. browser. documentCompleted + = new WebBrowserDocumentCompletedEventHandler (browser_DocumentCompleted);} private void browser_DocumentCompleted (object sender, WebBrowserDocumentCompletedEventArgs e) {(WebBrowser) sender ). document. window. error + = new HtmlElementErrorEventHandler (Window_Error);} private void Window_Error (object sender, HtmlElementErrorEventArgs e) {// ignore this Error and suppress Error dialog box e. handled = true;}/* He asked hovertree.com */
Recommended: http://www.cnblogs.com/roucheng/p/3521864.html