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.
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.
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.
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/
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With