在TWebBrowser中加载字符串(HTML代码)的最佳方法是什么? [英] Which is the best way to load a string (HTML code) in TWebBrowser?
问题描述
这是Embarcadero的代码:
procedure THTMLEdit.EditText(CONST HTMLCode:string);
{VAR
文件:IHTMLDocument2;
TempFile:string; }
begin
TempFile:= GetTempFile('。html');
StringToFile(TempFile,HTMLCode);
wbBrowser.Navigate(TempFile);
Doc:= GetDocument;
如果Doc<> NIL
然后Doc.Body.SetAttribute('contentEditable','true',0); //在这里加载复杂的html文件
DeleteFile(TempFile);
结束
它有一些问题,所以我用这个替换:
程序THTMLEdit.EditText(CONST HTMLCode:string);
VAR
TSL:TStringList;
MemStream:TMemoryStream;
begin
wbBrowser.Navigate('about:blank');
WHILE wbBrowser.ReadyState< READYSTATE_INTERACTIVE
DO Application.ProcessMessages;
GetDocument.DesignMode:='On';
如果分配(wbBrowser.Document)然后
begin
TSL:= TStringList.Create;
TRY
MemStream:= TMemoryStream.Create;
TRY
TSL.Text:= HTMLCode;
TSL.SaveToStream(MemStream);
MemStream.Seek(0,0);
(wbBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(MemStream));
FINALLY
MemStream.Free;
结束
FINALLY
TSL.Free;
结束
结束
结束
但是这个也有问题。首先,当我将链接(...)插入到HTML代码中时,浏览器会改变代码,在我的网址前添加'关于:'。第二:它比第一个程序(具有临时文件的程序)慢。
我可以在浏览器中加载HTML代码,而不首先导航到about:blank? / p>
您可以加载 HTML
代码如下
程序THTMLEdit.EditText(CONST HTMLCode:string);
var
Doc:Variant;
begin
if NOT Assigned(wbBrowser.Document)then
wbBrowser.Navigate('about:blank');
Doc:= wbBrowser.Document;
Doc.Clear;
Doc.Write(HTMLCode);
Doc.Close;
结束
I have a string var 'HTMLCode' that contains HTML code. I want to load this code into the browser.
This is Embarcadero's code:
procedure THTMLEdit.EditText(CONST HTMLCode: string);
{VAR
Doc: IHTMLDocument2;
TempFile: string; }
begin
TempFile := GetTempFile('.html');
StringToFile(TempFile, HTMLCode);
wbBrowser.Navigate(TempFile);
Doc := GetDocument;
if Doc <> NIL
then Doc.Body.SetAttribute('contentEditable', 'true', 0); //crash here when I load complex html files
DeleteFile(TempFile);
end;
It has some problems so I replaced it with this one:
procedure THTMLEdit.EditText(CONST HTMLCode: string);
VAR
TSL: TStringList;
MemStream: TMemoryStream;
begin
wbBrowser.Navigate('about:blank');
WHILE wbBrowser.ReadyState < READYSTATE_INTERACTIVE
DO Application.ProcessMessages;
GetDocument.DesignMode := 'On';
if Assigned(wbBrowser.Document) then
begin
TSL := TStringList.Create;
TRY
MemStream := TMemoryStream.Create;
TRY
TSL.Text := HTMLCode;
TSL.SaveToStream(MemStream);
MemStream.Seek(0, 0);
(wbBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(MemStream));
FINALLY
MemStream.Free;
end;
FINALLY
TSL.Free;
end;
end;
end;
But this one has problems also . First, when I insert links (...) into the HTML code, the browser will alter the code, appending 'about:' in front of my URLs. Second: it is slower than the first procedure (the one with temp file).
Can I load HTML code in browser without navigating first to 'about:blank'?
You could load your HTML
code as the below
procedure THTMLEdit.EditText(CONST HTMLCode: string);
var
Doc: Variant;
begin
if NOT Assigned(wbBrowser.Document) then
wbBrowser.Navigate('about:blank');
Doc := wbBrowser.Document;
Doc.Clear;
Doc.Write(HTMLCode);
Doc.Close;
end;
这篇关于在TWebBrowser中加载字符串(HTML代码)的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!