[ACCEPTED]-WebBrowser control and JavaScript errors-webbrowser-control

Accepted answer
Score: 15

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

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

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

Score: 11

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

You can make the 7 registry setting using the following c# code 6 (which sets IE10 mode if Windows 8 is detected) and 5 changing app-name.exe to match your own application. You 4 should add an error handler for the case 3 where there are insufficient privileges 2 (admin privileges are required to write 1 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);
Score: 10

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

webBrowser1.ScriptErrorsSuppressed = true;

It will prevent 1 getting JavaScript errors.

Score: 7

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

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

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

        <clear />
        <add name="X-UA-Compatible" value="IE=edge" />

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


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

Score: 3

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

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

Score: 2

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

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

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



More Related questions