Over a million developers have joined DZone.

Disable Javascript error in WPF WebBrowser control

DZone's Guide to

Disable Javascript error in WPF WebBrowser control

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

I work with WebBrowser control in WPF, and one of the most annoying problems I have with it, is that sometimes you browse sites that raise a lot of javascript errors and the control becomes 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.
<script type="text/javascript">

 function noError(){return true;}

 window.onerror = noError;


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.
private const string DisableScriptError =

    @"function noError() {

          return true;



       window.onerror = noError;";

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

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


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.
private void InjectDisableScript()


    HTMLDocumentClass doc = Browser.Document as HTMLDocumentClass;

    HTMLDocument doc2 = Browser.Document as HTMLDocument;



    //Questo crea lo script per la soprressione degli errori

    IHTMLScriptElement scriptErrorSuppressed = (IHTMLScriptElement)doc2.createElement("SCRIPT");
    scriptErrorSuppressed.type = "text/javascript";

    scriptErrorSuppressed.text = DisableScriptError;


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


    foreach (IHTMLElement elem in nodes)


       //Appendo lo script all'head cosi è attivo


      HTMLHeadElementClass head = (HTMLHeadElementClass)elem;




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.


Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.


Published at DZone with permission of Ricci Gian Maria, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}