Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebBrowser control and JavaScript errors

Tags:

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.

like image 992
user689792 Avatar asked Apr 03 '11 12:04

user689792


People also ask

How do I stop a script error in WebBrowser control?

In this case, set ScriptErrorsSuppressed to false and suppress script errors in a handler for the HtmlWindow. Error event. For more information, see the code example in this topic.

What is WebBrowser control?

The WebBrowser control provides a managed wrapper for the WebBrowser ActiveX control. The managed wrapper lets you display Web pages in your Windows Forms client applications.


2 Answers

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/

like image 77
Alton Crossley Avatar answered Dec 19 '22 08:12

Alton Crossley


If you have IE9 installed, the WebBrowser will still use IE7 mode unless you override this behaviour with a registry setting - as described in this StackOverflow answer. This is the most likely cause of the JavaScript errors you're getting in the WebBrowser (because you're not seeing the same errors in IE9).

You can make the registry setting using the following c# code (which sets IE10 mode if Windows 8 is detected) and changing app-name.exe to match your own application. You should add an error handler for the case where there are insufficient privileges (admin privileges are required to write to this registry key).

string installkey = @"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION"; string entryLabel = "app-name.exe"; System.OperatingSystem osInfo = System.Environment.OSVersion;  string version = osInfo.Version.Major.ToString() + '.' + osInfo.Version.Minor.ToString(); uint editFlag = (uint)((version == "6.2") ? 0x2710 : 0x2328); // 6.2 = Windows 8 and therefore IE10  RegistryKey existingSubKey = Registry.LocalMachine.OpenSubKey(installkey, false); // readonly key  if (existingSubKey.GetValue(entryLabel) == null) {      existingSubKey = Registry.LocalMachine.OpenSubKey(installkey, true); // writable key      existingSubKey.SetValue(entryLabel, unchecked((int)editFlag), RegistryValueKind.DWord); } 
like image 45
pgfearo Avatar answered Dec 19 '22 07:12

pgfearo