How can you programmatically detect if javascript is enabled/disabled in a Windows Desktop Application? (WebBrowser Control)

.net, c#, webbrowser-control, windows, winforms

Solution

Thanks to @Kickaha's suggestion. Here's a simple method which checks the registry to see if it's set. Probably some cases where this could throw an exception so be sure to handle them.

    const string DWORD_FOR_ACTIVE_SCRIPTING = "1400";
    const string VALUE_FOR_DISABLED = "3";
    const string VALUE_FOR_ENABLED = "0";

    public static bool IsJavascriptEnabled( )
    {
        bool retVal = true;
        //get the registry key for Zone 3(Internet Zone)
        Microsoft.Win32.RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3", true);

        if (key != null)
        {
            Object value = key.GetValue(DWORD_FOR_ACTIVE_SCRIPTING, VALUE_FOR_ENABLED);
            if( value.ToString().Equals(VALUE_FOR_DISABLED) )
            {
                retVal = false;
            }
        }
        return retVal;
    }

Note: in the interest of keep this code sample short (and because I only cared about the Internet Zone) - this method only checks the internet zone. You can modify the 3 at end of OpenSubKey line to change the zone.

Problem

I have an application which writes HTML to a WebBrowser control in a .NET winforms application. I want to detect somehow programatically if the Internet Settings have Javascript (or Active Scripting rather) disabled. I'm guessing you need to use something like WMI to query the IE Security Settings. EDIT #1: It's important I know if javascript is enabled prior to displaying the HTML so solutions which modify the DOM to display a warning or that use tags are not applicable in my particular case. In my case, if javascript isn't available i'll display content in a native RichTextBox instead and I also want to report whether JS is enabled back to the server application so I can tell the admin who sends alerts that 5% or 75% of users have JS enabled or not.

Original source