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

前端 未结 3 1351
我在风中等你
我在风中等你 2020-12-17 22:59

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 hav

3条回答
  •  悲&欢浪女
    2020-12-17 23:26

    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.

提交回复
热议问题