locked
WebBrowser control not supporting button click (Click event handled by JavaScript) RRS feed

  • Question

  • Hi,

    I added a WebBrowser control to a form and loading a web page for users to log in. There are 2 buttons in the page and the click event is handled by following JavaScript:

            function postOk()
            {
                document.forms[0]['pf.ok'].value = 'clicked';
                document.forms[0].submit();
            }
    
            function postCancel()
            {
                document.forms[0]['pf.cancel'].value = 'clicked';
                document.forms[0].submit();
            }
    
            function postOnReturn(e)
            {
                var keycode;
                if (window.event) keycode = window.event.keyCode;
                else if (e) keycode = e.which;
                else return true;
    
                if (keycode == 13)
                {
                    document.forms[0].submit();
                    return false;
                }
                else
                    return true;
             }

    My application uses .NET 4. The problem is, when I load the web page, even if the users click the buttons, nothing happens (page doesn't navigate). But interestingly, hitting "Enter" key works as expected.

    I found that WebBrowser control by default points to IE7 format and hence I made the registry change to include my application entry in FEATURE_BROWSER_EMULATION (under HKCU\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION) and set the value to point to IE9. Now only the double click on the buttons causes the page to navigate. I am not understanding why this behavior is observed. Any solution to this problem?

    Thanks in advance.



    Thanks Prasad


    • Edited by Prasad U S Tuesday, May 5, 2015 7:58 AM
    Tuesday, May 5, 2015 7:56 AM

Answers

  • I found out the problem and the solution. The code for buttons is as follows:

    <a href="javascript:void(0)" onclick="postOk();" class="button normal allow" title="Sign On">Sign On</a>
    <a href="javascript:void(0)" onclick="postCancel();" class="button blue deny" title="Cancel">Cancel</a>

    The above anchors contained href to JavaScript which is not supported by IE7+ (for WebBrowser control) as Microsoft considers it as a threat (as explained here). So nothing happens for the button clicks. To solve this problem, we need to add the following registry key:

    HKEY_LOCAL_MACHINE (or HKEY_CURRENT_USER)\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SCRIPTURL_MITIGATION\yourapplication.exe = 0x00000001 (DWORD)

    By default, this is enabled for IE, so to make it available to WebBrowser control, we need the above registry.


    Thanks Prasad

    • Proposed as answer by Armin Zingler Wednesday, May 6, 2015 10:57 AM
    • Marked as answer by Youjun Tang Thursday, May 14, 2015 3:31 AM
    Wednesday, May 6, 2015 10:02 AM

All replies

  • First try what the result is when using IE of your computer. The webbrowser is a reuse of that.

    Maybe you found an old message from the time IE7 was the latest.


    Success
    Cor

    Tuesday, May 5, 2015 8:46 AM
  • Hi Cor,

    Thanks for the reply.

    I tried the same URL on IE9 and it works fine and also my test machine has IE9 on it. I found the info on registry entry change from here:

    StackOverflow - Regarding IE9 WebBrowser control

    CodeProject - Configuring the emulation mode of an Internet Explorer WebBrowser control

    Make the WebBrowser Control give you the installed IE version rendering



    Thanks Prasad


    • Edited by Prasad U S Tuesday, May 5, 2015 12:57 PM
    Tuesday, May 5, 2015 12:49 PM
  • I found out the problem and the solution. The code for buttons is as follows:

    <a href="javascript:void(0)" onclick="postOk();" class="button normal allow" title="Sign On">Sign On</a>
    <a href="javascript:void(0)" onclick="postCancel();" class="button blue deny" title="Cancel">Cancel</a>

    The above anchors contained href to JavaScript which is not supported by IE7+ (for WebBrowser control) as Microsoft considers it as a threat (as explained here). So nothing happens for the button clicks. To solve this problem, we need to add the following registry key:

    HKEY_LOCAL_MACHINE (or HKEY_CURRENT_USER)\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SCRIPTURL_MITIGATION\yourapplication.exe = 0x00000001 (DWORD)

    By default, this is enabled for IE, so to make it available to WebBrowser control, we need the above registry.


    Thanks Prasad

    • Proposed as answer by Armin Zingler Wednesday, May 6, 2015 10:57 AM
    • Marked as answer by Youjun Tang Thursday, May 14, 2015 3:31 AM
    Wednesday, May 6, 2015 10:02 AM