16

I am experiencing Javascript errors with TWebbrowser due to the fact that TWebbrowser is running in IE7 compatibility mode.

Is there a way to prevent this and just have it run in IE9 mode?

kobik
  • 21,001
  • 4
  • 61
  • 121
user2638894
  • 303
  • 1
  • 2
  • 9
  • 2
    possible duplicate of [How do I turn off Compatibility View on the IE WebBrowserControl in a WinForms app?](http://stackoverflow.com/questions/6717055/how-do-i-turn-off-compatibility-view-on-the-ie-webbrowsercontrol-in-a-winforms-a) – whosrdaddy Sep 15 '14 at 09:08
  • Consider not relying on the right version of IE to be present, by using something like Chromium Embedded (lookup DCEF). – Thijs van Dien Sep 15 '14 at 09:22
  • 1
    @ThijsvanDien: Good point, but with DCEF you must ship external DLL's which is not always desired. – whosrdaddy Sep 15 '14 at 11:01

3 Answers3

12
  1. Opt in to the browser emulation feature using the documented registry key.
  2. Depending on the browser emulation setting that you selected, you may need to ensure that your document contains a suitable DOCTYPE. Again, this is described in the documentation.

So, for example, if you wish to make the simplest possible change you would add the following registry setting:

HKEY_LOCAL_MACHINE (or HKEY_CURRENT_USER)
   SOFTWARE
      Microsoft
         Internet Explorer
            Main
               FeatureControl
                  FEATURE_BROWSER_EMULATION
                     YourExeNameGoesHere.exe = (DWORD) 00009999

The documentation for the value 9999 says:

9999 Windows Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.

Were you to use 9000 then you'd need also to modify the DOCTYPE of your document:

9000 Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9.

The linked documentation also includes the information required to specify other IE versions.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 2
    TY. This solution works. For 32 bit applications on a 64 bit OS, you will need to add the entry under ...SOFTWARE\Wow6432Node\Microsoft\... – user2638894 Sep 15 '14 at 10:50
  • 2
    That's right. Unless you use `HKCU\Software` for which there are not separate 32 and 64 bit views. Of course, if you add the setting in your install program, then the registry redirector will do the work for you and redirect to the 32 bit view. Assuming a 32 bit installer for your 32 bit program. – David Heffernan Sep 15 '14 at 10:51
  • A bit off topic but stille relevant for you: If you are working with TwebBrowser this site could help you a lot : http://www.delphidabbler.com/articles?article=21 – Jens Borrisholt Sep 16 '14 at 06:53
  • @DavidHeffernan I recommend updating your answer with information about how you can make sure that TwebBrowser is running in latest emulation mode (IE11 for instance) since this seems to be the most commonly found answer regarding TWebBrowser and emulation mode problems in Delphi. I know the question is asking specifically for IE9 emulation but is it really necessary for someone to create a new question regarding the TWebBrowser IE11 emulation mode which would then have almost the same answer with only difference in DWORD value that needs to be used to achieve IE11 emulation mode – SilverWarior Aug 04 '16 at 02:02
  • Yes but nowhere in your question do you state that such approach can be also be used for emulating different versions of IE simply by using different DWORD values. – SilverWarior Aug 04 '16 at 15:27
  • @Silver well maybe but I'm of the opinion that we are here to help people learn and encourage their own research. If they aren't capable of reading the linked documentation and understanding it then I'm not interested in helping. – David Heffernan Aug 04 '16 at 15:29
  • 1
    @DavidHeffernan And on the other hand you often complain when other people link to offsite resources in their answers. Besides isn't the purpose of SO to provide answers to commonly asked questions and not pointes on how to reach those answers? – SilverWarior Aug 04 '16 at 15:42
  • @silver Links to offsite resources are fine so long as the answer is more than just that. Do you want me to copy the entire list and keep it up to date with all changes. – David Heffernan Aug 04 '16 at 15:54
  • @DavidHeffernan No! I'm merely suggesting that answer is modified so that it clear that linked documentation also provides information for emulating other Browser versions and not just IE9 which is specifically mentioned both in question and answer but no other browser versions are. – SilverWarior Aug 04 '16 at 16:01
8

Add this class to your code:

type TBrowserEmulationAdjuster = class
  private
      class function GetExeName(): String; inline;
   public const
      // Quelle: https://msdn.microsoft.com/library/ee330730.aspx, Stand: 2017-04-26
      IE11_default   = 11000;
      IE11_Quirks    = 11001;
      IE10_force     = 10001;
      IE10_default   = 10000;
      IE9_Quirks     = 9999;
      IE9_default    = 9000;
      /// <summary>
      /// Webpages containing standards-based !DOCTYPE directives are displayed in IE7
      /// Standards mode. Default value for applications hosting the WebBrowser Control.
      /// </summary>
      IE7_embedded   = 7000;
   public
      class procedure SetBrowserEmulationDWORD(const value: DWORD);
end platform;

class function TBrowserEmulationAdjuster.GetExeName(): String;
begin
    Result := TPath.GetFileName( ParamStr(0) );
end;

class procedure TBrowserEmulationAdjuster.SetBrowserEmulationDWORD(const value: DWORD);
const registryPath = 'Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION';
var
    registry:   TRegistry;
    exeName:   String;
begin
    exeName := GetExeName();

    registry := TRegistry.Create(KEY_SET_VALUE);
    try
       registry.RootKey := HKEY_CURRENT_USER;
       Win32Check( registry.OpenKey(registryPath, True) );
       registry.WriteInteger(exeName, value)
    finally
       registry.Destroy();
    end;

end;

Then add to your OnCreate of the Form:

TBrowserEmulationAdjuster.SetBrowserEmulationDWORD(TBrowserEmulationAdjuster.IE11_Quirks);

Thats all forever

Ingo
  • 5,239
  • 1
  • 30
  • 24
6

include in html, " http-equiv="X-UA-Compatible" content="IE=edge"

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" http-equiv="X-UA-Compatible" content="IE=edge" />
</head>
<body> 
                your code ....
</body>
</html>
  • Note that this meta tag must be the **first** tag in the head section, and ` ` must be set also. – kobik Aug 30 '17 at 16:31