DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • 5 Layers of Prompt Injection Defense You Can Wire Into Any Node.js App
  • Boosting React.js Development Productivity With Google Code Assist
  • Why Angular Performance Problems Are Often Backend Problems

Trending

  • Spring AI Advisors: Chat Memory, Token Tracking, and Message Logging
  • AI Assessments Are Everywhere
  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  • Stop Loading Everything into Redshift: A Spectrum + Iceberg Pattern for Hybrid Analytics
  1. DZone
  2. Coding
  3. JavaScript
  4. Disable Javascript error in WPF WebBrowser control

Disable Javascript error in WPF WebBrowser control

By 
Ricci Gian Maria user avatar
Ricci Gian Maria
·
Oct. 05, 10 · News
Likes (0)
Comment
Save
Tweet
Share
20.9K Views

Join the DZone community and get the full member experience.

Join For Free
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;

</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.
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)

 {

    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.
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;

      head.appendChild((IHTMLDOMNode)scriptErrorSuppressed);

    }

 }

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.


Windows Presentation Foundation JavaScript

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

Opinions expressed by DZone contributors are their own.

Related

  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • 5 Layers of Prompt Injection Defense You Can Wire Into Any Node.js App
  • Boosting React.js Development Productivity With Google Code Assist
  • Why Angular Performance Problems Are Often Backend Problems

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook