Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 502
blak3r Avatar asked Aug 29 '12 04:08

blak3r


People also ask

How check JavaScript is enabled or not in browser using PHP?

php //Check if we should check for js if ((! isset($_GET['jsEnabled']) || $_GET['jsEnabled'] == 'true') && ! isset($_SERVER['HTTP_X_REQUESTED_WITH'])){ //Check to see if we already found js enabled if (!

Do I have JavaScript disabled?

On the web browser menu click on the "Edit" and select "Preferences". In the "Preferences" window select the "Security" tab. In the "Security" tab section "Web content" mark the "Enable JavaScript" checkbox. Click on the "Reload the current page" button of the web browser to refresh the page.


2 Answers

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.

like image 56
blak3r Avatar answered Oct 26 '22 00:10

blak3r


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.

like image 45
Erx_VB.NExT.Coder Avatar answered Oct 25 '22 23:10

Erx_VB.NExT.Coder