locked
New WPF WebBrowser - How do I suppress script errors? RRS feed

  • Question

  • There is no ScriptErrorsSuppressed property, so how do I suppress them?

    It's extremely annoying that this control doesn't have this feature!
    Thursday, July 24, 2008 2:45 PM

Answers

  • Hi,

    Due to time constraints, we were very limited on how many features we could add to the WebBrowser control.  For now, the best option for any script which could throw a script error is to add a try/catch inside the failure-prone areas of the script.  If that's not possible, the next alternative would be to continue using the WinForms WebBrowser control, though that only works in Full Trust apps.

    Andre
    • Marked as answer by Marco Zhou Wednesday, July 30, 2008 11:22 AM
    Thursday, July 24, 2008 5:01 PM

All replies

  • Hi,

    Due to time constraints, we were very limited on how many features we could add to the WebBrowser control.  For now, the best option for any script which could throw a script error is to add a try/catch inside the failure-prone areas of the script.  If that's not possible, the next alternative would be to continue using the WinForms WebBrowser control, though that only works in Full Trust apps.

    Andre
    • Marked as answer by Marco Zhou Wednesday, July 30, 2008 11:22 AM
    Thursday, July 24, 2008 5:01 PM
  • It's April 2010 and still there is no ScriptErrorsSuppressed property. How long do I have to wait?

    Without it WebBrowser control is useless when displaying some badly made websites (although realtor.ca is
    a popular website) - Javascript errors crash application.

    Friday, April 2, 2010 12:32 PM
  • Totally agree! Do you guys care about the users? If so, don't make us wait another two years.
    Tuesday, April 13, 2010 4:41 AM
  • The problem is, if we  are using the WindowsFormsHost to host a WinForms WebBrowser control, we can SET the ScriptErrorsSuppressed but it still just does not have any effect. Even when running in full trust ...

    This is very annoying.

    Tuesday, April 27, 2010 5:41 PM
  • Still a major and unacceptable issue...  MSFT what's up?
    Javaman
    Friday, May 14, 2010 11:15 PM
  • I think there's little hope for a solution by MS so I thought I share my solution to work around this issue. It's a dirty hack but it works from a technical point-of-view (it works reliable and causes no issues on the performance side) and from a user point-of-view.

    I use a worker thread that looks for any messagebox/window/... that was created by the browser control window and destroy this popup immediately. It's fast and I think should do the job as good as possible.

    I have a ready-to-use solution on my blog, it's just a little class that does some interop. Also, there's an example how to call it in a multithreaded way. It's really easy (no additional coding required):

    http://www.bpk.cc/2010/5/16/a-very-ugly-(but-working)-alternative-to-scripterrorssuppressed-in-wpf-browser-control.aspx

     

    Saturday, May 15, 2010 11:14 PM
  • Here is a working solution. Using reflection one can set the property. God knows why MS has not done so allready.

    I run it on the webBrowser_Navigated event as the underlying activeX component is null until then.

     

     

     

    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 });

    }

     

     

     

    void webBrowser1_Navigated(object sender, NavigationEventArgs e) {

    HideScriptErrors(webBrowser1,

     

    true);

    }

    • Proposed as answer by Wolfie5 Tuesday, July 6, 2010 10:56 PM
    Tuesday, July 6, 2010 10:56 PM
  • Wolfie5,  Amazing solution.  This worked for me.  Thanks so much for imparting this knowledge.
    Wednesday, September 15, 2010 10:07 PM
  • Just to add to Wolfie5's already awesome solution, here's an extension method you can use:

      public static class WebBrowserExtensions
      {
        public static void SuppressScriptErrors(this WebBrowser webBrowser, bool hide)
        {
          FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
          if (fiComWebBrowser == null)
            return;
          object objComWebBrowser = fiComWebBrowser.GetValue(webBrowser);
          if (objComWebBrowser == null)
            return;
    
          objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { hide });
        }
      }
    

        

    Wednesday, September 15, 2010 10:25 PM
  • I am trying to use this solution with Silverlight...there is no _axIWebBrowser2 so should i be replacing it with the web browser name i gave?? Please help.
    Thursday, September 16, 2010 2:29 PM
  • Wolfie5 - This is a most excellent solution. :D Thanks a million for posting it!
    Thursday, September 16, 2010 6:53 PM
  • Wolfie5.. I LOVE YOU! xD

    actually my entire company loves you now... xDD  the best and most simply workaround for that annoying thing that could be solved very easy by microsoft engineers if the microsoft had fired some stupid stubborn engineers

    thank you very much!

    Friday, November 19, 2010 10:50 PM
  • This is awesome, thanks! 
    Tuesday, January 11, 2011 8:05 PM
  • Just to add to Wolfie5's already awesome solution, here's an extension method you can use:

     public static class WebBrowserExtensions
    
     {
    
      public static void SuppressScriptErrors(this WebBrowser webBrowser, bool hide)
    
      {
    
       FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
    
       if (fiComWebBrowser == null)
    
        return;
    
       object objComWebBrowser = fiComWebBrowser.GetValue(webBrowser);
    
       if (objComWebBrowser == null)
    
        return;
    
    
    
       objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { hide });
    
      }
    
     }
    
    

        


    This solution will not work when creating a System.Windows.WebBrowser from Code behind prior to navigation.  Reason: objComWebBrowser will always return null....

    Hey MSFT still trying to hang your own people?  For me, the System.Windows.Forms.Webbrowser won't work because I an navigating to a string.... Which can't be done in this browser until after a navigate to About:Blank.  Which requires a documentComplete handler, then setting the document text manually.


    JP
    Friday, April 29, 2011 2:44 AM
  • Here's a solution that works using code above but adding a Navigating event.  Prior to adding this event, there was a 70% chance of the Webbrowser value being null.

     

    void UpdateBrowser()
    {
        
    WebBrowser wb = new WebBrowser();           
        wb.Margin = 
    new Thickness(0, 5, 0, 0);
        wb.Navigating += 
    new NavigatingCancelEventHandler(wb_Navigating);
        wb.Height = 768;          
        wb.NavigateToString(SomeStringWithHTML);
     }
     
    void wb_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        
    WebBrowser wb = sender as WebBrowser;
        SuppressScriptErrors(wb, 
    true);
    }
    void SuppressScriptErrors(System.Windows.Controls.WebBrowser wb, bool Hide){
              
        
    FieldInfo fi = typeof(WebBrowser).GetField( "_axIWebBrowser2"BindingFlags.Instance | BindingFlags.NonPublic);        
        
    if (fi != null)    {        
            
    object browser = fi.GetValue(wb);            
            
    if (browser != null)        {
                browser.GetType().InvokeMember(
    "Silent"BindingFlags.SetProperty, null,browser, new object[] { Hide });            
            }    
        }


    JP
    Friday, April 29, 2011 3:01 AM
  • Wolfie's solution worked for me as well. Thanks! 


    Monday, May 30, 2011 7:38 PM
  • Wolfie's post should be marked as the answer.
    • Proposed as answer by studysake Tuesday, October 11, 2011 2:09 AM
    • Unproposed as answer by studysake Tuesday, October 11, 2011 2:09 AM
    Monday, May 30, 2011 7:40 PM
  • Thanks a lot Wolfie... Your solution brought me relief. My entire program was stuck at this very point. Now every thing is going smooth. My App is now complete.
    Tuesday, October 11, 2011 2:12 AM
  • hello

     

    Nice job there !

    But I'm actually using VB.net so I tried to convert wolfie5's code :

     

    Public Sub HideScriptErrors(ByVal wb As WebBrowser, ByVal Hide As Boolean)
    
            Dim fiComWebBrowser As FieldInfo = GetType(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance + BindingFlags.NonPublic)
            If (IsNothing(fiComWebBrowser)) Then Exit Sub
            Dim objComWebBrowser As Object = fiComWebBrowser.GetValue(wb)
            If IsNothing(objComWebBrowser) Then Exit Sub
            Dim args(1) As Object
            args(0) = Hide
            objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, Nothing, objComWebBrowser, args)
        End Sub
    
        Public Sub wb1navigated() Handles WebBrowser1.Navigated
            HideScriptErrors(WebBrowser1, True)
        End Sub
    

     


    But I get a TargetInvocationException on the last line of HideScriptErrors.

    Details say : {"Ne gère pas les groupes. (Exception de HRESULT : 0x80020011 (DISP_E_NOTACOLLECTION))"}

    which I guess would be in the English version : {Does not support a collection. (Exception from HRESULT: 0x80020011 (DISP_E_NOTACOLLECTION))}

     

    I have no knowledge in the C family so I guess my conversion was wrong on something.

    Any help ?

     

    Thank you


    • Edited by piher Thursday, October 13, 2011 3:55 PM
    Thursday, October 13, 2011 3:55 PM
  • There you go VB.NET users :

     

    Public Sub HideScriptErrors(ByVal wb As WebBrowser, ByVal Hide As Boolean)
            Dim fiComWebBrowser As FieldInfo = GetType(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance Or BindingFlags.NonPublic)
            If fiComWebBrowser Is Nothing Then
                Return
            End If
            Dim objComWebBrowser As Object = fiComWebBrowser.GetValue(wb)
            If objComWebBrowser Is Nothing Then
                Return
            End If
            objComWebBrowser.[GetType]().InvokeMember("Silent", BindingFlags.SetProperty, Nothing, objComWebBrowser, New Object() {Hide})
        End Sub
    
        Private Sub webBrowser1_Navigated(ByVal sender As Object, ByVal e As NavigationEventArgs) Handles WebBrowser1.Navigated
            HideScriptErrors(WebBrowser1, True)
        End Sub
    

    Enjoy,

     

    Thanks again wolfie5

    Friday, October 14, 2011 2:48 PM
  •  How does one use the HideScriptErrors sub routine? I don't understand where I would call this sub.

     

    Thanks


    • Edited by JimBassett Monday, October 24, 2011 7:23 PM
    Monday, October 24, 2011 7:20 PM
  • You obviously missed the end of wolfie5's code :

     

    void webBrowser1_Navigated(object sender, NavigationEventArgs e) {
    
    HideScriptErrors(webBrowser1, true);
    
    }
    

    The sub must be called by a sub handling the webbrowser1.navigated event.

    Monday, October 24, 2011 8:07 PM
  • Glad I stumbled across this. Thanks, Wolfie.
    Monday, January 9, 2012 10:05 PM
  • I hate to bash MSFT but.... The intolerable browser wrapper support for WPF WEBBROWSER forces us to get to know the C++ interfaces.  Little to no, or too much documentation makes it untenable.  So I've actually moved over to get this : Javascript and JQuery. 

    MSFT has this quirk were they do things half way, they give you ability to do it, but make it almost impossible to do simple things.  The Webbrowser should have been fully wrapped and maintained (for .NET Framwork).  When they introduced the WPF Browser they didn't do jack, forcing us to MSHTML side!  I find it unacceptable to have to backlevel to Windows.Forms.Webbrowser to do some of the things we could do with it.  At the same time learning the MSHTML interfaces is do-able but takes (sometimes) rocket science and lots of google searching to figure out. 

    Now that MSFT is so aminable to Javascript (Windows 8) hey it's time for us to bail.  BTW this thread is 4 years old, it indicates MSFT position on .NET Framework Browser wrappers.


    JP Cowboy Coders Unite!
    Tuesday, January 10, 2012 4:34 PM
  • Piher, your solution worked for me with VB.NET, however I needed to add the word Reflection before all the BindingFlags statements.
    Friday, May 25, 2012 1:54 AM
  • You can also add

    Imports System.Reflection

    On top of your class.

    I would recommend you take a few minutes to read about namespaces and imports, you will eventually have to use it a lot.

    Friday, May 25, 2012 10:47 AM
  • Thanks Wolfie5 . It worked like a charm
    Friday, May 31, 2013 11:09 PM
  • Hello, any hope to get a variation of your WPF solution working for Silverlight?

    The following returns null

    FieldInfofiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);

    Thank you.

    Wednesday, October 16, 2013 6:34 PM
  • Thanks a lot Wolfie5..... Its working .. :)

    Monday, February 10, 2014 9:43 AM
  • Well done Wolfie.. Impressive stuff.. Thanks a lot.
    Wednesday, February 19, 2014 1:53 PM
  • Thank You!

    This worked too awesome. Shame on MS. :(

    Friday, February 16, 2018 7:34 PM