WebBrowser.ScriptErrorsSuppressed 属性

定义

获取或设置一个值,该值指示 WebBrowser 是否显示对话框,如脚本错误消息。

C#
public bool ScriptErrorsSuppressed { get; set; }

属性值

如果控件不显示其对话框,则为 true;否则为 false。 默认值为 false

例外

WebBrowser 实例不再有效。

未能从基础 ActiveX WebBrowser control(WebBrowser 控件)检索到对 IWebBrowser2 interface(IWebBrowser2 接口)的实现的引用。

示例

下面的代码示例演示了如何在不禁止显示其他对话框的情况下禁止显示脚本错误。 在本示例中, ScriptErrorsSuppressed 属性设置为 false 以确保显示对话框。 事件的处理程序 HtmlWindow.Error 禁止显示错误。 仅当文档完成加载时,才能访问此事件,因此处理程序附加在事件处理程序中 DocumentCompleted

C#
// Hides script errors without hiding other dialog boxes.
private void SuppressScriptErrorsOnly(WebBrowser browser)
{
    // Ensure that ScriptErrorsSuppressed is set to false.
    browser.ScriptErrorsSuppressed = false;

    // Handle DocumentCompleted to gain access to 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;
}

注解

将此属性设置为 false 以调试控件中显示的 WebBrowser 网页。 使用 控件将基于 Web 的控件和脚本代码添加到应用程序时,这非常有用。 将 控件用作通用浏览器时,它不太有用。 调试完应用程序后,将此属性设置为 true 以禁止显示脚本错误。

备注

当 设置为 trueScriptErrorsSuppressed,控件WebBrowser将隐藏其源自基础 ActiveX 控件的所有对话框,而不仅仅是脚本错误。 有时,可能需要在显示对话框(例如用于浏览器安全设置和用户登录的对话框)时禁止显示脚本错误。 在这种情况下,请将 设置为 ScriptErrorsSuppressedfalse 并禁止在 事件的处理程序 HtmlWindow.Error 中出现脚本错误。 有关详细信息,请参阅本主题中的代码示例。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅