語言

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 控制項中取得 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 顯示的網頁。 當你用這個控制項來為應用程式新增基於網頁的控制項和腳本程式碼時,這非常有用。 當你把這個控制項當作通用瀏覽器使用時,它的實用性就比較低。 完成除錯後,將此屬性 true 設為抑制腳本錯誤。

Note

當 ScriptErrorsSuppressed 設為 true時, WebBrowser 控制項會隱藏所有來自底層 ActiveX 控制項的對話框,而非僅是腳本錯誤。 有時你可能需要在顯示對話框(如瀏覽器安全設定和使用者登入時)抑制腳本錯誤。 此時,將事件處理程序ScriptErrorsSuppressed設定false為 並HtmlWindow.Error抑制腳本錯誤。 欲了解更多資訊,請參閱本主題中的程式碼範例。

適用於

另請參閱