我正在使用tidhttp控件来加速Twebbrowser中网页的加载。直接导航到URL较慢,因此我不使用(WebBrowser1.Navigate('some_url_here')
)。这是我的做法:
procedure TForm1.Button2Click(Sender: TObject);
procedure LoadHtmlIntoBrowser(var WB: TweBbrowser; const HTMLString: string);
var
v: OleVariant;
HTMLDocument: IHTMLDocument2;
begin
WB.Navigate('about:blank');
while WB.ReadyState < READYSTATE_INTERACTIVE do
forms.Application.ProcessMessages;
if Assigned(WB.Document) then
begin
HTMLDocument := WB.Document as IHTMLDocument2;
v := VarArrayCreate([0, 0], varVariant);
v[0] := HTMLString;
HTMLDocument.Write(PSafeArray(TVarData(v).VArray));
HTMLDocument.Close;
end;
forms.Application.ProcessMessages;
end;
var
str:string;
begin
str:=idhttp1.Get('http://localhost/myhome.html');
LoadHtmlIntoBrowser(WebBrowser1,str);
end;
我使用
idHTTP
将HTML内容获取到一个字符串中,然后直接将该字符串写入Webbrowser。我设置了本地Web服务器(XAMPP)。问题在于,在HTML内容被写入浏览器后,当我单击显示的链接时,它不起作用,即显示大多数空白页面,并在顶部显示“twopage.html”。右键单击并选择“查看源代码”,我得到了"<html>twopage.html</html>"
,这很奇怪,不是实际页面的HTML。“myhome.html”文件包含:
<html>
<head></head>
<body><h1>My home</h1><a href="twopage.html"></a></body>
</html>
另一个网页"twopage.html"包含以下内容:
<html>
<head></head>
<body><h1>Another Webpage</h1></body>
</html>
http://somewhere
时它通常所做的那样。我不知道您是否可以“通知”它基本网址是什么,但您肯定可以更改“myhome.html”文件以包括绝对链接,例如<a href="http://localhost/twopage.html">
,我敢打赌这会在您的设置中起作用。另一方面,我真的不明白这如何显着提高页面加载时间,并且我不确定它是否适用于涉及相关资源(例如CSS、图像等)的复杂页面。 - jachguate可能甚至不可能
。 - TLama<base>
标签,以便Web浏览器具有基本URL来解析相对链接。 - Remy Lebeau