I work with WebBrowser control in WPF, and one of the most annoying problem I have with it, is that sometimes you browse sites that raise a lot of javascript errors and the control become unusable. Thanks to my friend Marco Campi, yesterday I solved the problem. Marco pointed me a link that does not deal with WebBrowser control, but uses a simple javascript script to disable error handling in a web page.

   1: <script type="text/javascript">
   1:  

   2:  

   3: function noError(){return true;}

   4: window.onerror = noError;

   5:  

</script>

This solution is really simple, and seems to me the right way to solve the problem. The key to the solution is handle the Navigated event raised from the WebBrowser control. First of all I have my WebBrowser control wrapped in a custom class to add functionalities, in that class I declare this constant

   1: private const string DisableScriptError =

   2:    @"function noError() {

   3:          return true;

   4:       }

   5:        

   6:       window.onerror = noError;";

This is the very same script of the previous article, then I handle the Navigated event

   1: void browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)

   2: {

   3:    InjectDisableScript();

Actually I’m doing a lot of other things inside the Navigated event handler, but the very first one is injecting into the page the script that disable javascript error.

   1: private void InjectDisableScript()

   2: {

   3:    HTMLDocumentClass doc = Browser.Document as HTMLDocumentClass;

   4:    HTMLDocument doc2 = Browser.Document as HTMLDocument;

   5:  

   6:  

   7:    //Questo crea lo script per la soprressione degli errori

   8:    IHTMLScriptElement scriptErrorSuppressed = (IHTMLScriptElement)doc2.createElement("SCRIPT");

   9:    scriptErrorSuppressed.type = "text/javascript";

  10:    scriptErrorSuppressed.text = DisableScriptError;

  11:  

  12:    IHTMLElementCollection nodes = doc.getElementsByTagName("head");

  13:  

  14:    foreach (IHTMLElement elem in nodes)

  15:    {

  16:       //Appendo lo script all'head cosi è attivo

  17:  

  18:       HTMLHeadElementClass head = (HTMLHeadElementClass)elem;

  19:       head.appendChild((IHTMLDOMNode)scriptErrorSuppressed);

  20:    }

  21: }

This is the code that really solves the problem, the key is creating a IHTMLScriptElement with the script and injecting into the Head of the page, this effectively disables the javascript errors. I’ve not fully tested with a lot of sites to verify that is able to intercept all errors, but it seems to work very well with a lot of links that gave us a lot of problems in the past.

Alk.

Tags:

Tags:

Tags: ,

4 Responses to “Disable Javascript error in WPF WebBrowser control”

  1. HTMLDocumentClass is not available in wpf there is no reference which uses that

  2. Is from MSHTML COM component :)

Trackbacks/Pingbacks

  1. Help the bottom of the page on Facebook keeps showing the lock thing? | Facebook Help
  2. security groups launch effort to save the radar