Allow System.Windows.Forms.WebBrowser to run javascript

45,293

Set the property ScriptErrorsSuppressed of the WebBrowser control to true to suppress the JavaScript error message.


In order to allow the code on hulu.com to execute, you must run the Webbrowser control in a mode such that it runs with newer version features. This can only be done by setting registry entries.

See this question and the answers for details.


To specify: I have a demo application to open the hulu website with the embedded WebBrowser control named WindowsFormsApplication5.exe.

Without registry changes, I see a note by Hulu that JavaScript support is not enabled. When sniffing the network transfer with Fiddler, I see that the following request is sent to the Hulu server:

GET http://www.hulu.com/ HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
Accept-Language: de-DE,de;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Creative AutoUpdate v1.41.09)
Host: www.hulu.com
Connection: Keep-Alive
Pragma: no-cache

Note the version number "7.0" in the User-Agent string.

I now add a registry key of type REG_DWORD with name "WindowsFormsApplication5.exe" and value 0x00002af9 (11001) in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION.

As a result, the Hulu Website is successfully displayed in my demo application and I see the following request being sent via Fiddler:

GET http://www.hulu.com/ HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
Referer: http://www.hulu.com/
Accept-Language: de-DE,de;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: www.hulu.com
Connection: Keep-Alive

Notice the different User-Agent string after the registry changes.


Registry setting to use IE 11 in WebBrowser Control in Demo Application

Demo Application showing Hulu Website after registry changes

Share:
45,293
rlranft
Author by

rlranft

I spend most of my time writing infrastructure utilities (fun stuff like active directory tools, file copy utilities, simple data miners).

Updated on June 19, 2020

Comments

  • rlranft
    rlranft almost 4 years

    Seriously - read the question all the way through before attempting to answer it. "Use a different browser" doesn't answer this question.

    The question is this: How do I get a System.Windows.Forms.WebBrowser control to display a web page that has Javascript embedded in it when I have no control over the incoming page? I've already seen people suggest adding registry values for IE emulation (Allowing javascript to run on a windows form web browser) and altering the script settings in Internet Options/Security. Internet security options

    I've done that already.

    So, here's the test - use a System.Windows.Forms.WebBrowser control to access www.hulu.com.
    Still getting this

    And we're still getting this.

    Any other ideas?

  • Xavier J
    Xavier J over 7 years
    This doesn't fix operability of sites that require Javascript. It's just hiding the error messages.
  • rlranft
    rlranft over 7 years
    1) Using a different browser defeats the purpose of the question.... 2) As I stated before " I've already seen people suggest adding registry values for IE emulation (Allowing javascript to run on a windows form web browser) and altering the script settings in Internet Options/Security." and "I've done that already."
  • NineBerry
    NineBerry over 7 years
    @rlranft The proposed registry changes in the question linked to in your question and the one I linked to work. Try them again.
  • rlranft
    rlranft over 7 years
    "Try them again...?" What, do they disappear? I took those screenshots after applying those changes and restarting the machine in question. Those registry entries and settings are still active.
  • Xavier J
    Xavier J over 7 years
    The WebBrowser control does not "emulate" IE - it IS IE. which is why that went right past me. The WebBrowser control is an ugly stepchild of an ActiveX control that's been around since IE4 (yes, I've been doing this a long time). There are no ways to pull off what you're attempting to do beside registry changes, because Javascript is a security concern in certain environments (think high-security places such as military installations or nuclear power plants), and that's why MS left no options to programatically enable it. Hence my suggestion to try a different browser.
  • rlranft
    rlranft over 7 years
    I'm also aware it doesn't "emulate" IE, and I also have been doing this for a few minutes - since summer of 1992, actually (ActiveX wasn't even a dream yet). The reason using another browser doesn't answer the question is because the question wasn't "what browser should I use to correctly handle web pages with javascript."
  • rlranft
    rlranft over 7 years
    You know what? I'm accepting this answer - which is basically "you can't." Thanks for the input, I actually appreciate it - and I hope Microsoft fixes their garbage soon.
  • Xavier J
    Xavier J over 7 years
    @rlranft Their approach with this is no different than the Java Runtime disclaimer (at installation time) to not use it in aircraft or similar facilities. In the case of an incident, the attorneys would pulverize Microsoft to bits over leaving such a gaping security hole in their base product (installed with Windows!). The Javascript on/off switch didn't come around til IE6 or so. Good luck.
  • NineBerry
    NineBerry over 7 years
    @rlranft See my extended answer. The registry changes DO change how the embedded WebBrowser control works. I have used this before and now tested it again to confirm it works to Show the Hulu website.
  • Reza Aghaei
    Reza Aghaei over 7 years
    @codenoir You don't need to use a different control. You need to tell WebBrowser control to don't use compatibility mode and use the latest version of IE document mode. Then it shows all modern contents well. Take a look at this post.
  • Xavier J
    Xavier J over 7 years
    @RezaAghaei if your system is configured to not run Javascript in IE, compatibility mode is not magically going to change that. Compatibility mode is for rendering HTML elements, and has nothing to do with Javascript.
  • Reza Aghaei
    Reza Aghaei over 7 years
    Your system never is set to not run javascripts. In such cases you can see the site without any problem using Internet Explorer, but using WebBrowser Control you just receive errors and a broken rendered page. The problem is with showing modern content and running modern javascripts in WebBrowser control which is set to not use latest IE document mode by default. Just check the solution which I described in linked post.
  • Xavier J
    Xavier J over 7 years
    @RezaAghaei You are 100% wrong. You have apparently missed the mention of the entire 'Security Settings' dialog at the top of this question. This dialog has available on EVERY windows box for years, and can be accessed from Control Panel or inside IE. The default is to allow Javascript, but it CAN be turned off. Go to Control Panel, Internet Properties. Find the "Security" tab. Click "Custom Level". See?
  • Reza Aghaei
    Reza Aghaei over 7 years
    The dialog which you see in the question is what the user tried to solve the problem which is obviously wrong solution. I know you can change security settings, but you don't need in 99% cases. Just pay attention to the script which is trying to run cdn.optimizely.com.... Then you will know you don't need to change security settings. In most cases you will see the site is OK in IE11, but using WebBrowser control you can't see the site.
  • Reza Aghaei
    Reza Aghaei over 7 years
    You're answer is partially true, but I shared this comment and the linked post with you just to know what the problem is and how you can setup WebBrowser control to show modern contents.
  • Xavier J
    Xavier J over 7 years
    @RezaAghaei again, you're missing the context. The OP is writing an application to be installed in places where he may not have control of whether Javascript is turned on or off in IE, and was seeking a workaround.
  • Xavier J
    Xavier J over 7 years
    If the site the control goes to doesn't need Javascript, then it's not a problem.
  • Reza Aghaei
    Reza Aghaei over 7 years
    The user is trying to browse hulu.com . You can try to see the site in IE then in WebBrowser control. Then you will understand what I am talking about :)
  • Reza Aghaei
    Reza Aghaei over 7 years
    In those dialog screenshots, the user is telling while I have set all scripting features enabled, why I receive script errors. Did you get it?
  • Reza Aghaei
    Reza Aghaei over 7 years
    Did you get it? I tried what I said and I get expected result. Let me know if you had any problem applying the solution.
  • rlranft
    rlranft over 7 years
    Your extended answer still doesn't work for me. I have updated the values for my application in HKEY_CURRENT_USER\Software\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION, HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION, and HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION. I still get the "your software is blocking javascript" message
  • rlranft
    rlranft over 7 years
    This method fails for me on 64 bit Windows 7 and 64 bit Windows 10.
  • NineBerry
    NineBerry over 7 years
    @rlranft Do NOT use the path with Wow6432Node! My test was in Windows 10 64bit.
  • rlranft
    rlranft over 7 years
    Okay, I take it all back. This is working for www.funimation.com but not www.hulu.com for me where both were failing to load before. Thanks.
  • NineBerry
    NineBerry over 7 years
    Works for hulu.com as well over here and there is no reason why it shouldn't work when doing everything correctly.
  • rlranft
    rlranft over 7 years
    Never fear - I only used the Wow6432Node keys on 64 bit systems.... Define "doing everything correctly" here - I'll have to wait until later to verify this on Win10 and I'm on Win7 at work (all 64 bit - I don't think I'll need to run this on 32 bit anything).
  • rlranft
    rlranft over 7 years
    Or are you saying that, contrary to the instructions you linked, I should not use the Wow6432Node keys regardless of my system architecture?