Tuesday, June 11, 2013

Windows VisualStuduio WebBrowser Control Does Not Run JavaScript


You have dragged in the Web Browser Control into your Windows desktop application. Then you tried to open a page that contains JavaScript. You immediately get messages like "JSON" not found, or other sorts of script error.

You have already checked to see if JavaScript. Enable property is available but there is not that you can see.

Root Cause:

Please know the following facts;
  • By default the WebBrowser control does not support JavaScript, and also it runs in IE 7 compatibility mode.
  • The WebBrowser control is nothing but the same version if IE running on your desktop. So if your system only has the IE 8 then it will only run up to the feature of IE 8.
  • It is not true that you change the your IE's security mode the Javascript will run. Reason for this is that the security model is tied to the name of the program set up in the registry. The same settings actually control iexplore.exe as well.
  • If you are running a program via Visual Studio in debugging mode, the name of the program is different from the EXE that it produces. 
 Suggested Fixes
  • Because the program name changes depending on how you are running it, I suggest that you automate the program name detection using the  following code snippet.
The following code snippet allows you to configure the registry settings so that the web browser control will assume a proper IE mode of operation. This is by default set to IE 7.0 without JavaScript. The following example sets the IE to IE 9 emulation mode and also detects a program name you are running as.


      private static void SetIE9Feature()
                UInt32 FeatureCode = 9000; // for IE9, use 8000 for IE8 and 7000 for IE7 etc.   
                var asm = System.Reflection.Assembly.GetExecutingAssembly();
                var rootKey = Registry.LocalMachine;
                var sk = rootKey.OpenSubKey(@"SOFTWARE", true);
                var mk = sk.OpenSubKey(@"Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BEHAVIORS", true);
                if (mk != null)
                    var p = System.AppDomain.CurrentDomain.FriendlyName;
                    mk.SetValue(p, FeatureCode, RegistryValueKind.DWord);

                var mk2 = sk.OpenSubKey(@"Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",             
                if (mk2 != null)
                    var p = System.AppDomain.CurrentDomain.FriendlyName;
                    mk2.SetValue(p, FeatureCode, RegistryValueKind.DWord);

                var m = String.Format(@"Great! Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BEHAVIORS and " +                 
                    "FEATURE_BROWSER_EMULATION modes went OK to mode {0}. Please restart the " +
                    " application for this to take effect.", 

            catch(Exception ex)
                var m = String.Format(@"Sorry, it's a NO GO for setting Microsoft\Internet " +
                "Explorer\Main\FeatureControl\FEATURE_BEHAVIORS and FEATURE_BROWSER_EMULATION modes. "+ 
                "Did you run as Administrator? {0}",

No comments: