When IE browser encountered a script error, in the bottom left corner of the browser will appear a yellow icon, click to view the details of the script error, there will be no pop-up error message box. We are using the program written by WebBrowser to open the Web page, encountered a script problem is, will pop up an error box, you need to confirm before you can perform. If the program we are designing is designed to automate the processing of Web pages, when this happens, the program is interrupted and requires human intervention. This obviously does not meet our requirements.
So how do you handle a script error when you use WebBrowser to open a Web page to allow the program to run without interference automatically?
WebBrowser provides us with a property: Scripterrorssuppressed. You can set this value to True when you do not want to encounter script errors again when you pop up the error balloon.
Webbrowser1.scripterrorssuppressed = true;
The specific usage of the scripterrorssuppressed attribute is as follows:
Set this property to False to debug the Web page that is displayed in the WebBrowser control. This property is useful if you want to use the control to add web-based controls and scripting code to your application. This property is not useful if you use the control as a generic browser. When you have finished debugging your application, set this property to true to suppress script errors.
Note: When scripterrorssuppressed is set to True, the WebBrowser control hides all dialog boxes from the underlying ActiveX control, not just script errors. Sometimes, when you display some dialog boxes (for example, dialog boxes for browser security settings and user logons), you may need to suppress script errors. In this case, you should set the scripterrorssuppressed to false and suppress the script error in the handler for the Htmlwindow.error event.
The negative effect of this approach, as described above, if you only want to mask script errors, you can use the following methods:
The following code shows how to suppress a script error without canceling the display of another dialog box. In this example, set the Scripterrorssuppressed property to false to ensure that the dialog box is displayed. The handler for the Htmlwindow.error event suppresses the error. This event can only be accessed if the document has finished loading, so the handler is attached to the DocumentCompleted event handler.
View Plaincopy to Clipboardprint?
Hide script errors only, other errors are still prompted
private void Suppressscripterrorsonly (WebBrowser browser)
{
Make sure scripterrorssuppressed is set to false.
Browser. scripterrorssuppressed = false;
Handles 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 the error and suppress the error dialog box
E.handled = true;
}