Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Tidhttp with Twebbrowser

I am using the tidhttp control to speed up the loading of a webpage in a Twebbrowser. Navigating to the url is slow so thats why I don't use it(WebBrowser1.Navigate('some_url_here')). Here is how i do it:

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;

I use the idHTTP to get the html content into a string then write that string directly to Webbrowser. I have a local webserver setup(XAMPP). The problem I having is that after the html content has been written to the browser and I click a link that is displayed, it goes no where i.e. it shows a mostly blank page with "twopage.html" at the top. When I right click and "view source" i get "<html>twopage.html</html>" which is strange and not the actual html of the page.

The "myhome.html" file contains

<html>
  <head></head>
  <body><h1>My home</h1><a href="twopage.html"></a></body>
</html>

The other webpage, "twopage.html" contains

<html>
  <head></head>
  <body><h1>Another Webpage</h1></body>
</html>
like image 728
megatr0n Avatar asked Mar 15 '13 20:03

megatr0n


1 Answers

You need to insert a <base> tag into the HTML before loading it into the web browser, so that it has a base url available when resolving relative urls.

like image 132
Remy Lebeau Avatar answered Sep 25 '22 15:09

Remy Lebeau