23
votes

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.

6

6 Answers

15
votes

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/

11
votes

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);
}
10
votes

You can use the following code line to get rid of those types of errors:

webBrowser1.ScriptErrorsSuppressed = true;

It will prevent getting JavaScript errors.

7
votes

So i know the post is old, but it was a recent problem for me and i had to do some serious digging and thinking outside the box.

basically like most replies here - you cannot alter the webbrowser control to use the most recent IE engine. mine uses IE7 by default, i have seen some replies that basically changes/ adds stuff to registry, am always not comfy when it comes to the registry, a cleaner way to address this issue would be to append code on your website that forces it to use the most current IE engine on any pc and works like a charm.

if you have access to the web.config file of the page you intend to browse, simple append:

    <system.webServer>
<httpProtocol>
    <customHeaders>
        <clear />
        <add name="X-UA-Compatible" value="IE=edge" />
    </customHeaders>
</httpProtocol>
</system.webServer>

and your site would force webbrowser control to run the most current IE engine on your computer. Other options are found here:

https://www.leapinggorilla.com/Blog/Read/1016/ie-ate-my-css---disabling-compatability-mode

I should state that this would only work if you have access to the page / web.config of the website/ application you are trying to access- which was my case.

3
votes

THe WebBrowser control uses IE7. So if there is a problem then your script does not work for IE7 and you will have to fix that.

You cannot integrate IE9 as it depends on it being installed on the computer and not everyone has IE9 installed.

2
votes

As a help to whoever else may have this problem, I tried all these things and nothing worked for me. Here's what does work. I am not sure exactly what causes this error, but apparently when you just press "F5" in VS to debug your app, it runs YourProject.vshost.exe as the process name. If you run the same app from the command line, it will show up as YourProject.exe, and the javascript errors vanish. I think IE sees the app running visa via VSHOST and decides this is fishy and disables javascript from loading correctly.

So... go into your project setting for your executable. Select "Debug" options. Select "Start External Program". Browse to and select Debug\YourProgram.exe (NOT YourProgram.vshost.exe). Save, recompile, and hit F5.

Everything should work as per usual now, and Visual Studio even attaches to the process for you automatically.

Enjoy!

Grego