Disabling web page scripting errors in the integrated WebBrowser component in WPF applications

Posted: (EET/GMT+2)

 

If you are using the integrated WebBrowser component in your WPF (Windows Presentation Foundation) applications, you might have seen script error popup windows being displayed. The error dialog boxes look like this:

Script Error
An error has occurred in the script on this page.

Line: 0
Char: 0
Error: Script error
Code: 0
URL: http://fi.pliing.com/PliingAdFx/GetAd?id=1234567

Do you want to continue running scripts on this page?

Yes   No

This dialog boxes are shown when there are errors in the JavaScript code of the page you are currently displaying in the integrated WebBrowser component. If you want to keep your application's user interface consistent, you probably want to disable or hide such popup error messages.

I was in a situation like this earlier this week, and initially, I was looking for a simple property to hide these errors. In Windows Forms (WinForms) programming, there's a property called ScriptErrorsSuppressed that neatly does the trick. However, it turns out the WPF component counterpart doesn't have this property, so you have to figure out something else.

The solution in WPF applications is to access the internal ActiveX component inside the WebBrowser control, and then set a property called Silent, which suppresses such dialog box. This property is part of the IWebBrowser2 interface that the WPF's control is using internally.

Now then, how do you set this property? Here is an example in C#:

using System.Windows.Controls;
using System.Reflection;
... 

private void HideWebBrowserScriptErrors(WebBrowser browserComponent)
{
    // use Reflection to access internal non-public members of the integrated web browser
    Type browserType = browserComponent.GetType();
    dynamic browserField = browserType.GetField("_axIWebBrowser2",
        BindingFlags.Instance | BindingFlags.NonPublic);

    if (browserField == null)
    {
        MessageBox.Show("Could not access browser's ActiveX implementation");
        return;
    }

    dynamic fieldValue = browserField.GetValue(browserComponent);
    if (fieldValue != null)
    {
        Type fieldType = fieldValue.GetType();
        fieldType.InvokeMember("Silent", BindingFlags.SetProperty, null,
            fieldValue, new Object[] { true });
        MessageBox.Show("Script error dialogs have now been disabled");
    }
    else
    {
        MessageBox.Show("Could not access browser's ActiveX property values");
    }
} 

To call this method, simply pass in the instance of the web browser component of which error's your want to disable. You can call this method before you navigate to the first page (url) with the particular browser component. For instance, you could call this method once in the window/page loaded event handler in your WPF application.

The Microsoft Knowledge Base article 261003 talks about this from a native code perspective.

Finally, just add your own error handling, and you're good to go!

Happy hacking, hope this helps!