Thursday, July 29, 2010

Disable/Enable Script Debugging in Internet Explorer programmatically


Hello everybody,
Currently I was involved in a project where I had to disable script debugging from registry.
After a lot of searching and googling I came across some thing like this:







UI Location:
Tools->Internet Options->Advanced->Browsing
Registry:
Location: HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main
Browsing: Disable script debugging (Internet Explorer)
Name: DisableScriptDebuggerIE
Type: REG_SZ
Data: yes (checked in the UI) or no (unchecked in the UI)

Browsing: Disable script debugging (Other)
Name: Disable Script Debugger
Type: REG_SZ
Data: yes (checked in the UI) or no (unchecked in the UI)
But for me, I had to do it programmatically so I started writing the code and after some issues and problems, It worked. :) .
I came across number of similar questions on other forums. Owing to my big heart, I decided to blog it and here I am:
I am not going in much detail as starting from create a Website or web Application or windows appluication and blah blah blah!
Assuming you are all smart programmers, I would start with creating a button and working on its click.
In the design page: 



Just create a button on the page with text as Disable Script Debugging and id as 


btnDisableJavascript




In the code page:


Namespace: using Microsoft.Win32;

   //Click of btnDisableJavascript
    protected void btnDisableJavascript_Click(object sender, EventArgs e)
    {
        bool enable = false;
        
        if (btnDisableJavascript.Text == "Disable Javascript")
        {
            enable = false;
            btnDisableJavascript.Text="Enable Javascript";
        }
        else
        {
            enable = true;
            btnDisableJavascript.Text="Disable Javascript";
        }
        EnableDisableJavaScript(enable);
    }


    //Disable/Enable Javascript from code(Registry):
    public static void EnableDisableJavaScript(bool enable)
    {
        //get the registry key for your local system
        RegistryKey regKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\Main", true);
        if (regKey != null)
        {
            try
            {
                //if you want to disable script debugging
                if (enable == false)
                { 
                    //set REG_SEZ for Disable Script Debugger(Internet Explorer) to yes
                    regKey.SetValue("DisableScriptDebuggerIE", "yes");
                    //set REG_SEZ for Disable Script Debugger(Other) to yes
                    regKey.SetValue("Disable Script Debugger", "yes");
                }
                else //if you want to enable script debugging
                {
                    //set REG_SEZ for Disable Script Debugger(Internet Explorer) to no
                    regKey.SetValue("DisableScriptDebuggerIE", "no");
                    //set REG_SEZ for Disable Script Debugger(Other) to no
                    regKey.SetValue("Disable Script Debugger", "no");
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message, null);
            }
        }
    }


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

Click this button, after processing is complete, go to Tools>Internet Options>Advanced Tab:


you can see the checkboxes for Disable Script Debugger(Internet Explorer) and Disable Script Debugger(Other)are checked as below

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

Click this button now and after processing is complete, go to Tools>Internet Options>Advanced Tab:

you can see the checkboxes for Disable Script Debugger(Internet Explorer) and Disable Script Debugger(Other)are unchecked as below: 


And thats a yippi and happy case.
As our target is achieved.

Thats all for "Disable/Enable Script Debugging in Internet Explorer programmatically".

Catch you all some other day with some other knowledge stuff.