9
votes

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.

3
@blak3r check out my answer, it will solve your problems. - Erx_VB.NExT.Coder
@Erx_VB.NExT.Coder Thanks for the good answer. You were 5 hours too late though :/ The answer I posted takes the same approach you outlined to check the registry. +1'd your answer for completeness. - blak3r
@blak3r glad its worked for you, just keep in mind, with regediting, you cant change JS on/off status on the fly, ie: whenever you change you need to either restart app or do a proper unload and reload in and out of memory (the WB control that is). but if that is not a problem for you, then all is dandy :) - Erx_VB.NExT.Coder
@Erx_VB.NExT.Coder Yep. I'm not modifying the user's computer. That's evil. Just want to detect or not. - blak3r

3 Answers

5
votes

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.

2
votes

If you are having troubles with popups popping up, i've included a solution for you, and if you want to disable/enable javascript on th client machine (or even just read/query if it is enabled/disabled) ive included that answer for you as well, here we go:

Which popup message do you want to disable? If it's the alert message, try this, obviously resolving the window or frame object to your particular needs, I’ve just assumed top-level document, but if you need an iframe you can access it using window.frames(0). for the first frame and so on... (re the JavaScript part)... here is some code, assuming WB is your webbrowser control...

WB.Document.parentWindow.execScript "window.alert = function () { };", "JScript"

You must run the above code only after the entire page is done loading, i understand this is very difficult to do (and a full-proof version hasn't been published yet) however I have been doing it (full proof) for some time now, and you can gather hints on how to do this accurately if you read some of my previous answers labelled "webbrowser" and "webbrowser-control", but getting back to the question at hand, if you want to cancel the .confirm JavaScript message, just replace window.alert with window.confirm (of course, qualifying your window. object with the correct object to reach the document hierarchy you are working with). You can also disable the .print method with the above technique and the new IE9 .prompt method as well.

If you want to disable JavaScript entirely, you can use the registry to do this, and you must make the registry change before the webbrowser control loads into memory, and every time you change it (on & off) you must reload the webbrowser control out and into memory (or just restart your application).

The registry key is \HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\ - the keyname is 1400 and the value to disable it is 3, and to enable it is 0.

Of course, because there are 5 zones under the Zones key, you need to either change it for the active zone or for all zones to be sure. However, you really don't need to do this if all you want to do is supress js dialog popup messages.

Let me know how you go, and if I can help further.

1
votes

Here is a suggestion - Encode the warning into your webpage as default. Create a javascript that runs on page load which removes that element. The warning will be there when ever javascript is not allowed to run.

It's a long while since I coded client side HTML javascript to interact with the DOM so I may be a little out of date. i.e. you will need to fix details, but I hope I get the general idea across.

<script>
document.getElemntByID("noJavascriptWarning").innerHTML="";
</script>

and in your HTML body

<DIV id="noJavascriptWarning" name="noJavaScriptWarning">Please enable Javascript</DIV>