WebBrowser.ScriptErrorsSuppressed 属性

定义

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

public:
 property bool ScriptErrorsSuppressed { bool get(); void set(bool value); };
public bool ScriptErrorsSuppressed { get; set; }
member this.ScriptErrorsSuppressed : bool with get, set
Public Property ScriptErrorsSuppressed As Boolean

属性值

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

例外

WebBrowser 实例不再有效。

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

示例

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

// 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;
}
' Hides script errors without hiding other dialog boxes.
Private Sub SuppressScriptErrorsOnly(ByVal browser As WebBrowser)

    ' Ensure that ScriptErrorsSuppressed is set to false.
    browser.ScriptErrorsSuppressed = False

    ' Handle DocumentCompleted to gain access to the Document object.
    AddHandler browser.DocumentCompleted, _
        AddressOf browser_DocumentCompleted

End Sub

Private Sub browser_DocumentCompleted(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs)

    AddHandler CType(sender, WebBrowser).Document.Window.Error, _
        AddressOf Window_Error

End Sub

Private Sub Window_Error(ByVal sender As Object, _
    ByVal e As HtmlElementErrorEventArgs)

    ' Ignore the error and suppress the error dialog box. 
    e.Handled = True

End Sub

注解

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

注意

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

适用于

另请参阅