mercredi 28 septembre 2016

How to insert correct link in TWebBrowser?

I have a HTML page (MyFile.html) located in c:\SiteRoot\MyFile.html. I want to insert a link to it using:

Doc.execCommand('createlink', true, 0);

Since MyFile.html is directly in the root of the site, the HTML code for the link should be:

<a href="MyFile.html">Link</a>

However, there are three problems:

  1. The code above will insert 'Windows' paths:

c:\SiteRoot\MyFile.html

instead of

c:/SiteRoot/MyFile.html

  1. The second issue you probably spoted already: it inserts the full path including 'C:' instead of inserting paths relative to site root (so, the link should simply be href="MyFile.html").

I got rid of both problems above by replacing the default 'Link editor' window with my own. I build the HTML code on my own and insert the code in TWebBrowser and using:

procedure THTMLEdit.InsertHtml(HTMLCode: string);   
VAR
  Doc: IHTMLDocument2;
  iSelection: IHTMLSelectionObject;
  iRange: IHTMLTxtRange;
begin
  Doc := wbBrowser.Document as IHTMLDocument2;
  iSelection := Doc.selection as IHTMLSelectionObject;
  iRange := iSelection.createRange() as IHTMLTxtRange;
  iRange.pasteHTML(HTMLCode);
end;

However, this is where the third problem appears. The string that I pass to this procedure is <a href="MyFile.html">Link</a> but in the resulted HTML I get: <A href="file:///C:/MyFile.html">Link</A>

How to stop TWebBrowser from inserting 'file:///C:/' ?




Aucun commentaire:

Enregistrer un commentaire