How do I open a Web page with WebBrowser script error __web

Source: Internet
Author: User
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.