Monday, 9 September 2013

Disable/Enable Javascript in Internet Explorer programmatically









In the design page:       


 Just create a button on the page with text as Disable Javascript and name as                                                                                                    Rajesh Kumar 
                                                             Rajeshkrit@gmail.com
                                                         http://www.fivebrain.com
                                                                                                                      http://www.reado.com/
                                                                   

btnDisableJavascript

In the code page:

Namespace: using Microsoft.Win32;

   //Click of btnDisableJavascript
    protected void btnDisableJavascript_Click(object sender, EventArgs e)
    {
         bool disable = false;

        if (btnDisableJavascript.Text == "Disable Javascript")
        {
            disable = true;
            btnDisableJavascript.Text = "Enable Javascript";
        }
        else
        {
            disable = true;
            btnDisableJavascript.Text = "Disable Javascript";
        }
        EnableDisableJavaScript(disable);
    }

    //Disable/Enable Javascript from code(Registry):
    public static void EnableDisableJavaScript(bool enable)
    {
         //get the registry key for Zone 3(Internet Zone)
        RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3", true);
        //get the registry key for Zone 1(Local Intranet Zone)
        RegistryKey keyLocalFiles = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1", true);
        //get the registry key for Zone 1(My Computer)
        RegistryKey keyMyComputer = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\0", true);
        if (value == true)
        {
            try
            {
                if (key != null)
                {
                    //1400 is the DWORD for Scripting: Active scripting
                    //setting of "3" prohibits the specific action i.e. disable for this case
                    key.SetValue("1400", "3", RegistryValueKind.DWord);
                    keyLocalFiles.SetValue("1400", "3", RegistryValueKind.DWord);
                    keyMyComputer.SetValue("1400", "3", RegistryValueKind.DWord);
                }
                else
                {
                    key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3");
                    keyLocalFiles = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1");
                    keyMyComputer = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\0", true);
                    //1400 is the DWORD for Scripting: Active scripting
                    //setting of "3" prohibits the specific action i.e. disable for this case
                    key.SetValue("1400", "3", RegistryValueKind.DWord);
                    keyLocalFiles.SetValue("1400", "3", RegistryValueKind.DWord);
                    keyMyComputer.SetValue("1400", "3", RegistryValueKind.DWord);
                }

            }
            catch (Exception ex)
            {

            }
        }
        else
        {
            try
            {
                if (key != null)
                {
                    //setting of "0" sets a specific action as permitted i.e. enable for this case
                    key.SetValue("1400", "0", RegistryValueKind.DWord);
                    keyLocalFiles.SetValue("1400", "0", RegistryValueKind.DWord);
                    keyMyComputer.SetValue("1400", "0", RegistryValueKind.DWord);
                }
                else
                {
                    key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3");
                    keyLocalFiles = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1");
                    keyMyComputer = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\0");
                    //1400 is the DWORD for Scripting: Active scripting
                    //setting of "0" sets a specific action as permitted i.e. enable for this case
                    key.SetValue("1400", "0", RegistryValueKind.DWord);
                    keyLocalFiles.SetValue("1400", "0", RegistryValueKind.DWord);
                    keyMyComputer.SetValue("1400", "0", RegistryValueKind.DWord);
                }

            }
            catch (Exception ex)
            {

            }
        }
    }

When you'll run, the disable javascript button is displayed as follows:

Click this button, after processing is complete, go to Tools>Internet Options< Active Scripting

you can see the checkbox for Disable is checked as below

and the Text of the button is changed to Enable Javascript:

3 comments:

  1. Wow..really path breaking article..It has immensely helped me a lot as i was having huge problem earlier but now with the help of this great "technocrat" it has become so easy to manage javascript in IE..!
    Hats off to you sir...keep up the good work.

    ReplyDelete