WebBrowser control and JavaScript errors

user689792 picture user689792 · Apr 3, 2011 · Viewed 42k times · Source

When I access the page with the browser (ie9), the browser is rendering ok.

When I use the WebBrowser control I have JavaScript errors.

I know I can suppress the scripts errors, but I want them to run correctly, because they affect the rendering and the functionality of the page.

How can I solve this problem ? Can I integrate IE9 directly in the Windows Form and use similar methods like with the WebBrowser control (navigate,get id, invoke click) ?

Thanks.

Answer

Alton Crossley picture Alton Crossley · Aug 30, 2011

What I would do is assign an object to webbrowser.ObjectForScripting and then inject a javascript function that assigns windown.onerror to a wrapper that calls the external script in the host app. Like:

window.onerror = function(message, url, lineNumber) 
{ 
  window.external.errorHandler(message, url, lineNumber);
}

Refere to: http://notions.okuda.ca/2009/06/11/calling-javascript-in-a-webbrowser-control-from-c/