title: HtmlWindow.Error Not Catching Script Errors .NET
body: <p>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. </p>
  
  <p>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. </p>
  
  <pre class="lang-vb prettyprint-override"><code>    ' 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
  </code></pre>
  
  <p>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.) </p>
  
  <p>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 <strong>4.0</strong>, <strong>VS2012</strong> on Windows 7 Professional. </p>
  
  <p>I've tried this both in VB.NET and C#, both having the same issue.</p>
  
  <p>You can go to this site here <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.scripterrorssuppressed.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.scripterrorssuppressed.aspx</a> </p>