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 belowThe simplest way to display HTML code in Delphi:
your Qustions:
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'?
Answers:
We start with code and the first procedure (only to show where the about: ...) comes from.
We have created 2 files (are identical) only script differs to get an alert at load.
bearbeiten1.html
bearbeiten3.html
With click on Load File we load the "bearbeiten1.html" file
and with WB_LoadHTML we load it into Memory.
we get URL : about:blank
and the alert
now we create a link:
we select the blue part and click createlink
The link is created
and also the new "Doc.body.innerHTML"
So far so good ! But will it work...? No
After a click on the link all we get is a blank site with URL:
Now we try the new EditText() code
With click on Load File we load the "bearbeiten1.html" file again and with EditTextBtnClick we load it direct. Looks much better! Will it work ...?
Let's click the link ! We get the alert !! from Nr. ...3.html"
and the .html file is loaded without problem.
To your other problem
You did it at the wrong place the body is only available after the site is loaded !!
So I put it in the event WebBrowserNavigateComplete2
Only a fast solution can be improved
The complete code.
UPDATE:
I forgot to set the Tempfile Path in the code (copy paste error).
Also FormCreate added.
and only one time load the TempFile twice! (see code)
Important in the head tag of the TempFile must be a link
bearbeiten1.html same as bearbeiten3.html only
alert ("bearbeiten3.html");
must be adapted !!bearbeiten1.html
maor.css