0

I have created a windows form application which houses a webbrowser control. I am having an issue with it showing a scripting error when using the code below.

I want to catch the scripting errors, suppress them and log them to a file. Below is the EXACT code that the msdn documentation tells me to use.

    ' 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

I wait until the documentCompleted event to attach the window.error handler. This however does not work as expected, it's like the handler never gets registered. (When going through and console logging throughout the flow it turns out that it throws the scripting error BEFORE the document has completed, this happens during the navigated event.)

Once I click OK in the scripting error window it then fires the documentCompleted event. Does anyone have any suggestions? I have tried to attach the handler in the navigation, navigating, document Completed events. Also, the tricky part here is that the document MUST be completely loaded before I can even add the handler, anytime before and it doesn't work. I'm using 4.0, VS2012 on Windows 7 Professional.

I've tried this both in VB.NET and C#, both having the same issue.

You can go to this site here http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.scripterrorssuppressed.aspx

1 Answer 1

3

You are declaring the handler once the document has completed loading so the event will never get fired. You have to declare the Handler before the document has completed rendering.

declare this:

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

At application startup or wherever, but before and out of the navigation completed scope.

Sign up to request clarification or add additional context in comments.

1 Comment

+1. Perhaps, WebBrowser.Navigated event is the right place for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.