繁体   English   中英

Delphi TWebBrowser无法从LocalHost运行Javascript

[英]Delphi TWebBrowser Wont Run Javascript from LocalHost

我有一个非常简单的Delphi XE7程序。 基本上,它只是一个TWebBrowser组件,嵌入在窗体中,没有附加任何代码,只是触发了Browser.Navigate方法的按钮之外。 我的理解是TWebBrowser只是IE的ActiveX包装器。

我试图用它来显示一个引用D3 Javascript库的非常简单的页面(但到目前为止,它没有任何作用),并且这些网页是通过使用WAMPSERVER在我的PC上运行的localhost网络服务器提供的。

网页在Chrome或IE 11(我的Windows 7为64位)中运行良好。 但是,当我尝试在Delphi / TWebBrowser程序中查看它们时,收到IE错误消息“此页面上的脚本发生了错误”(请参见下图)。 尝试访问本地主机上d3test / d3文件夹中的d3.js javascript库时,似乎发生了错误。 我已经验证了d3.js文件确实存在于此文件夹中,并且该页面在Chrome和IE中都可以正常运行并显示得很好,这似乎证明了这一点。

使用嵌入式Web浏览器访问本地托管的页面可能存在问题? 其他背景-我还清除了IE缓存,在Windows控制面板上重置了Internet选项,将IE安全设置设置为最低级别,并暂时禁用了Norton Firewall / Virus扫描程序。

有人对此有任何想法吗? 我真的很希望能够在基于Windows的程序中嵌入一些D3图表。

这也是html代码:

<!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>

在此处输入图片说明

我在问题下方的评论中添加了答案,因此可能对其他人有帮助

将此元标记添加到您的网页中

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

在这种情况下,您应该将此类添加到代码中:

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;

结束;

最后添加到您的窗体的OnCreate中:

TBrowserEmulationAdjuster.SetBrowserEmulationDWORD(TBrowserEmulationAdjuster.IE11_Quirks);

这应该可以解决您的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM