HtmlWindow.Error HtmlWindow.Error HtmlWindow.Error HtmlWindow.Error Event

定义

当窗口内部运行的脚本遇到运行时错误时发生。Occurs when script running inside of the window encounters a run-time error.

public:
 event System::Windows::Forms::HtmlElementErrorEventHandler ^ Error;
public event System.Windows.Forms.HtmlElementErrorEventHandler Error;
member this.Error : System.Windows.Forms.HtmlElementErrorEventHandler 
Public Custom Event Error As HtmlElementErrorEventHandler 

示例

下面的代码示例捕获结果在 HTML 页面上脚本尝试访问文档中未定义的对象的错误。The following code example traps the error that results when a script on an HTML page attempts to access an object that is not defined in the document. 页面必须是完全加载之前Error附加事件处理程序,否则该示例将不起作用。The page must be fully loaded before the Error event handler is attached, otherwise the example will not work.

private void SuppressScriptErrors()
{
          if (webBrowser1.Document != null)
          {
              webBrowser1.Document.Window.Error += new HtmlElementErrorEventHandler(scriptWindow_Error);
          }
      }

      private void  scriptWindow_Error(object sender, HtmlElementErrorEventArgs e)
      {
          MessageBox.Show("Suppressed error!");
          e.Handled = true;
      }
Dim WithEvents ScriptWindow As HtmlWindow

Private Sub SuppressScriptErrors()
    If (WebBrowser1.Document IsNot Nothing) Then
        ScriptWindow = WebBrowser1.Document.Window
    End If
End Sub

Private Sub ScriptWindow_Error(ByVal sender As Object, ByVal e As HtmlElementErrorEventArgs) Handles ScriptWindow.Error
    MessageBox.Show("Suppressed error!")
    e.Handled = True
End Sub

注解

HTML 页面可以包含脚本代码,通常用 JScript 或 VBScript 中,加载页面时执行。HTML pages can contain script code, usually written in JScript or VBScript, that executes when a page is loaded. Error 每当脚本遇到运行时错误时发生。Error occurs whenever a script encounters a run-time error. 由于脚本代码是后期绑定,这意味着,对对象的调用不被解析直到运行时错误可能包括从引用 null 对象到调用未定义的属性或方法的所有内容。Because script code is late-bound, which means that calls against the object are not resolved until run-time, errors can include everything from referencing a null object to calling an undefined property or method.

可以设置Handled属性的HtmlElementErrorEventArgs为 true,以便防止在 Internet Explorer 中的本机错误对话框中显示。You can set the Handled property of HtmlElementErrorEventArgs to true in order to prevent the native error dialog box in Internet Explorer from displaying.

适用于

另请参阅