4

I have a really simple Delphi XE7 program. It is basically just a TWebBrowser component embedded in a form with no extra code attached, other than a button that fires off the Browser.Navigate method. My understanding is that TWebBrowser is just an ActiveX wrapper for IE.

I am trying to use this to display a very simple page that references the D3 Javascript library (but so far doesn't do anything with it), and the web pages are served from a localhost webserver that is running on my PC using WAMPSERVER.

The web pages run just fine in Chrome or IE 11 (I have Windows 7, 64 bit). But when I try to view them within the Delphi/TWebBrowser program I get the IE error message "An error has occurred on the script on this page" (see image attached). The error seems to occur when trying to access the d3.js javascript library in the d3test/d3 folder on the local host. I have verified that the d3.js file does exist in this folder and this seems to be borne out by the fact that the page runs and displays just fine in both Chrome and IE.

Perhaps there is an issue with having an embedded web browser access locally hosted pages? Additional background -I have also cleared the IE cache, reset the Internet options on the Windows Control Panel, set IE security settings to the minimum level and temporarily disable my Norton Firewall/Virus scanner.

Does anyone have any thoughts on this? I'm really hoping to be able to get some D3 charts embedded in my Windows-based program.

Here also is the html code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 Test</title>
<script type="text/javascript" src="d3\d3.js"></script>
</head>
<body>
Hello World
</body>
</html>

enter image description here

Lars Kotthoff
  • 107,425
  • 16
  • 204
  • 204
CHEAPS
  • 179
  • 3
  • 13
  • 3
    I belive the problem might be the fact that by default any embeded WebBrowser2 Windows (TWebBrowser uses them) runs in IE7 compatibility mode. Check http://stackoverflow.com/a/25843958/3636228 for workaround – SilverWarior Jun 11 '15 at 12:44
  • 1
    Many thanks! As is typical, I also found out (just after posting the above) that I can fix the problem by adding the following to the html code: – CHEAPS Jun 11 '15 at 12:48
  • 1
    @CHEAPS: yes you can add the metatag, but the correct solution is to add the registry key. You can add it to HKCU so your program should have no problems creating this key... – whosrdaddy Jun 11 '15 at 13:27
  • OK - will do - thanks everyone! – CHEAPS Jun 11 '15 at 13:49
  • 2
    Technically speaking, Microsoft does not formally support registry settings and makes no guarantee that any particular setting will be supported in the next release (or even after the next patch/update). The `x-ua-compatible` header, however, is formally documented and supported for IE8 through IE11. (In IE 11, the feature has been deprecated, which is a formal step that eventually leads to future non-support.) Practically speaking, the regkey works, but requires an update to each workstation whereas the header is added once. The correct answer should vary according to individual need. – Lance Leonard Jun 11 '15 at 17:54

2 Answers2

7

I added answer from your comments below the question so its may helpful to others

add this meta tag into your web page

<meta http-equiv="X-UA-Compatible" content="IE=edge" />
Azad
  • 5,144
  • 4
  • 28
  • 56
2

In this case you should 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;

Finaly add to your OnCreate of the Form:

TBrowserEmulationAdjuster.SetBrowserEmulationDWORD(TBrowserEmulationAdjuster.IE11_Quirks);

This should solve your problem

Ingo
  • 5,239
  • 1
  • 30
  • 24