简体   繁体   English

Delphi TWebBrowser无法从LocalHost运行Javascript

[英]Delphi TWebBrowser Wont Run Javascript from LocalHost

I have a really simple Delphi XE7 program. 我有一个非常简单的Delphi XE7程序。 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. 基本上,它只是一个TWebBrowser组件,嵌入在窗体中,没有附加任何代码,只是触发了Browser.Navigate方法的按钮之外。 My understanding is that TWebBrowser is just an ActiveX wrapper for IE. 我的理解是TWebBrowser只是IE的ActiveX包装器。

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

The web pages run just fine in Chrome or IE 11 (I have Windows 7, 64 bit). 网页在Chrome或IE 11(我的Windows 7为64位)中运行良好。 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). 但是,当我尝试在Delphi / TWebBrowser程序中查看它们时,收到IE错误消息“此页面上的脚本发生了错误”(请参见下图)。 The error seems to occur when trying to access the d3.js javascript library in the d3test/d3 folder on the local host. 尝试访问本地主机上d3test / d3文件夹中的d3.js javascript库时,似乎发生了错误。 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. 我已经验证了d3.js文件确实存在于此文件夹中,并且该页面在Chrome和IE中都可以正常运行并显示得很好,这似乎证明了这一点。

Perhaps there is an issue with having an embedded web browser access locally hosted pages? 使用嵌入式Web浏览器访问本地托管的页面可能存在问题? 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. 其他背景-我还清除了IE缓存,在Windows控制面板上重置了Internet选项,将IE安全设置设置为最低级别,并暂时禁用了Norton Firewall / Virus扫描程序。

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. 我真的很希望能够在基于Windows的程序中嵌入一些D3图表。

Here also is the html code: 这也是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>

在此处输入图片说明

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" />

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: 最后添加到您的窗体的OnCreate中:

TBrowserEmulationAdjuster.SetBrowserEmulationDWORD(TBrowserEmulationAdjuster.IE11_Quirks);

This should solve your problem 这应该可以解决您的问题

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

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