5
votes

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>
1
Your web browser have no "base url" to properly construct the relative links the way it normally does when you really navigate to http://somewhere. I don't know if you can inform it what that "base url" is, but you for sure can change your "myhome.html" file to include absolute links, for example <a href="http://localhost/twopage.html">, which I bet will work in your setup. On the other hand, I really don't see how this can improve the time to load the page in a significant way, and I'm not sure if it works with a complex page involving related resources, like css, images and the suchjachguate
I agree with @jachguate, maybe it's not even possible without at least one real navigation to the desired location.TLama
I am changing the links to absolute. This does not solve the problem but only works around it. This will do for now. Thanks.megatr0n
@Jachguate, I expect that this specific code and the local server are just for testing. With a remote server, this could be useful for "pre-loading" some pages. However, if that's the intention, then we wouldn't really care whether TWebBrowser can directly navigate to the other links since we'd want to fetch them from the pre-load cache instead. If it's not just for testing, then using absolute links isn't much of a workaround since the whole thing feels like a workaround for some other problem.Rob Kennedy
@jachguate: You can insert a <base> tag into the HTML before loading it into the webbrowser, so that the webbrowser has a base url to resolve relative links with.Remy Lebeau

1 Answers

7
votes

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.