Wednesday, June 12, 2013

Windows Visual Studio WebControl Under IE 7 Emulation Does Not Load JSON

Symptom:

Under the IE7 emulation mode of the Visual Studio WebControl, JSON does not load.  You receive 'JSON' is undefined error.

Root Cause:

JSON is not natively supported in IE7.

Workaround:

You can include JSON2 from https://github.com/douglascrockford/JSON-js/blob/master/json2.js in your script set and this will make JSON to work.




Windows Visual Studio WebControl Does Not Load the Latest JavaScript Version

Symptom

You have enabled the JavaScript capability on the WebControl using my previous artcile (read now). However, the JavaScript version stays with, for example, version 13 instead of version 17, that's standard on IE 9.

Root Cause

This appears to be due to some extra stuff in !DOCTYPE attribute of a page. By default Visual Studio 2010 new Web Forms page will add the following

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"

It is most likely that  xhtml1-transitional.dtd is affecting the browser behavior but I am not going to verify that (you can.)

Fix

If you keep the !DOCTYPE only to just to say , in other words remove the rest of the junk in the DOCTYPE, then the JavaScript version will go up to the latest version.

The HTML5 standard requires that the DOCTYPE to only have html as its attribute.

Tuesday, June 11, 2013

Windows VisualStuduio WebBrowser Control Does Not Run JavaScript

Symptom:

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.


      using Microsoft.Win32;  // DO NOT FORGET TO INCLUDE THIS ON TOP OF YOUR PROGRAM

      private static void SetIE9Feature()
        {
            try
            {  
                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",             
                                        true);
                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.", 
                    FeatureCode);
                    MessageBox.Show(m);

            }
            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}",
                    ex.ToString());
                    MessageBox.Show(m);
            }
        }