How do I suppress script errors when using the WPF WebBrowser control?

WpfBrowser

Wpf Problem Overview


I have a WPF application that uses the WPF WebBrowser control to display interesting web pages to our developers on a flatscreen display (like a news feed).

The trouble is that I occasionally get a HTML script error that pops up a nasty IE error message asking if I would like to "stop running scripts on this page". Is there a way to suppress this error checking?

NOTE: I have disabled script debugging in IE settings already.

Wpf Solutions


Solution 1 - Wpf

Here is a solution i just made with reflection. Solves the issue :) I run it at the Navigated event, as it seems the activeX object is not available until then.

What it does is set the .Silent property on the underlying activeX object. Which is the same as the .ScriptErrorsSuppressed property which is the Windows forms equivalent.

 public void HideScriptErrors(WebBrowser wb, bool Hide) {
    FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
    if (fiComWebBrowser == null) return;
    object objComWebBrowser = fiComWebBrowser.GetValue(wb);
    if (objComWebBrowser == null) return;
    objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { Hide });
 }

A better version that can be run anytime and not after the .Navigated event:

public void HideScriptErrors(WebBrowser wb, bool hide) {
    var fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
    if (fiComWebBrowser == null) return;
    var objComWebBrowser = fiComWebBrowser.GetValue(wb);
    if (objComWebBrowser == null) {
        wb.Loaded += (o, s) => HideScriptErrors(wb, hide); //In case we are to early
        return;
    }
    objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { hide });
}

If any issues with the second sample, try swapping wb.Loaded with wb.Navigated.

Solution 2 - Wpf

Just found from another question, this is elegant and works great.

dynamic activeX = this.webBrowser1.GetType().InvokeMember("ActiveXInstance",
                BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
                null, this.webBrowser1, new object[] { });

activeX.Silent = true;

Solution 3 - Wpf

The problem here is that the WPF WebBrowser did not implement this property as in the 2.0 control.

Your best bet is to use a WindowsFormsHost in your WPF application and use the 2.0's WebBrowser property: SuppressScriptErrors. Even then, you will need the application to be full trust in order to do this.

Not what one would call ideal, but it's pretty much the only option currently.

Solution 4 - Wpf

I've also found an interesting way to disable JavaScript errors. But you need to use at least .Net Framework 4.0 because of using elegant dynamic type.

You need to subscribe to the LoadCompleted event of the WebBrowser element:

<WebBrowser x:Name="Browser" 
            LoadCompleted="Browser_OnLoadCompleted" />

After that you need to write an event handler that looks like below:

    void Browser_OnLoadCompleted(object sender, NavigationEventArgs e)
    {
        var browser = sender as WebBrowser;

        if (browser == null || browser.Document == null)
            return;

        dynamic document = browser.Document;

        if (document.readyState != "complete")
            return;

        dynamic script = document.createElement("script");
        script.type = @"text/javascript";
        script.text = @"window.onerror = function(msg,url,line){return true;}";
        document.head.appendChild(script);
    }

Solution 5 - Wpf

I wanted to add this as a comment to @Alkampfer answer, but I don't have enough reputation. This works for me (Windows 8.1, NET 4.5):

 window.Browser.LoadCompleted.Add(fun _ -> 
    window.Browser.Source <- new System.Uri("javascript:window.onerror=function(msg,url,line){return true;};void(0);"))

This code sample is written in F#, but it's pretty clear what it does.

Solution 6 - Wpf

I've this problem in the past and finally resolved it with an injection of a Javascript script that suppress error handling. Hope this could help you too.

Disable Javascript errors in WEbBrowsercontrol

Solution 7 - Wpf

Check the below code for suppressing script errors for WPF browser control..

    public MainWindow
    {
    InitializeComponent();
    WebBrowserControlView.Navigate(new Uri("https://www.hotmail.com"));
                        //The below checks for script errors.
    ViewerWebBrowserControlView.Navigated += ViewerWebBrowserControlView_Navigated;
    }
    
void ViewerWebBrowserControlView_Navigated(object sender, NavigationEventArgs e)
            {
    BrowserHandler.SetSilent(ViewerWebBrowserControlView, true); // make it silent
            }

public static class BrowserHandler
{
    private const string IWebBrowserAppGUID = "0002DF05-0000-0000-C000-000000000046";
    private const string IWebBrowser2GUID = "D30C1661-CDAF-11d0-8A3E-00C04FC9E26E";

    public static void SetSilent(System.Windows.Controls.WebBrowser browser, bool silent)
    {
        if (browser == null)
            MessageBox.Show("No Internet Connection");

        // get an IWebBrowser2 from the document
        IOleServiceProvider sp = browser.Document as IOleServiceProvider;
        if (sp != null)
        {
            Guid IID_IWebBrowserApp = new Guid(IWebBrowserAppGUID);
            Guid IID_IWebBrowser2 = new Guid(IWebBrowser2GUID);

            object webBrowser;
            sp.QueryService(ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out webBrowser);
            if (webBrowser != null)
            {
                webBrowser.GetType().InvokeMember("Silent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.PutDispProperty, null, webBrowser, new object[] { silent });
            }
        }
    }

}

[ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleServiceProvider
{
    [PreserveSig]
    int QueryService([In] ref Guid guidService, [In] ref Guid riid, [MarshalAs(UnmanagedType.IDispatch)] out object ppvObject);
 

   }

Whereas, If you are using Winforms Web browser with winforms host.. you have a property "SuppressScriptErrors" set it to true

    <WindowsFormsHost Name="WinformsHost" Grid.Row="1">
    <winForms:WebBrowser x:Name="WebBrowserControlView" ScriptErrorsSuppressed="True" AllowWebBrowserDrop="False"></winForms:WebBrowser>
</WindowsFormsHost>

Solution 8 - Wpf

you can use this trick

vb.net

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const WM_CLOSE As Short = &H10s

and call last lib :

 dim hwnd 
    dim vreturnvalue
    hwnd = FindWindow(vbNullString,"script error")
    if hwnd<>0 then	vreturnvalue = SendMessage(hwnd, WM_CLOSE,  &O0s, &O0s)

Solution 9 - Wpf

Wolf5's answer is good but if (objComWebBrowser == null) doesn't seem to work. Instead I check the WebBrowser instance for IsLoaded:

public void HideScriptErrors(WebBrowser webBrowser)
{
	if (!webBrowser.IsLoaded)
	{
		webBrowser.Loaded += WebBrowser_Loaded; // in case we are too early
		return;
	}

	var objComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(webBrowser);
	if (objComWebBrowser == null)
	{
		return;
	}

	objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { true });
}
private void WebBrowser_Loaded(object sender, RoutedEventArgs e)
{
	var webBrowser = sender as WebBrowser;
	webBrowser.Loaded -= WebBrowser_Loaded;
	HideScriptErrors(webBrowser);
}

It is also necessary to remove the Loaded event handler after the first time, as the control may be unloaded and loaded again several times when making it invisible by switching to a different tab. Also the if (!wb.Loaded)) fallback is still important when the WebBrowser is not visible yet on its first navigation, e.g. if it is on a different tab that is not visible on application startup.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwillemView Question on Stackoverflow
Solution 1 - WpfWolf5View Answer on Stackoverflow
Solution 2 - WpfCarolView Answer on Stackoverflow
Solution 3 - WpfKyle RosendoView Answer on Stackoverflow
Solution 4 - Wpfuser1826289View Answer on Stackoverflow
Solution 5 - WpfalexbView Answer on Stackoverflow
Solution 6 - WpfAlkampferView Answer on Stackoverflow
Solution 7 - WpfDareyView Answer on Stackoverflow
Solution 8 - WpfMwaffak Jamal ZakariyaView Answer on Stackoverflow
Solution 9 - WpfKim HomannView Answer on Stackoverflow