Search code examples
delphidelphi-xe

Delphi - How to make a TEdit label the TWebBrowser curently url


I'm creating a webbrowser and I use a TWebBrowser and a TEdit to type in the url.But when I open a web page with a link then when I click that link, I goes to an another web page and my question is how to make the TEdit label the currently url.


Solution

  • Write a handler for the OnNavigateComplete2 event:

    procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject;
      const pDisp: IDispatch; const URL: OleVariant);
    begin
      Edit1.Text := URL;
    end;
    

    The documentation says:

    Write an OnNavigateComplete2 event handler to take specific action when the Web browser successfully navigates to a new resource. The event can occur before the document is fully downloaded, but when it occurs at least part of the document must be received and a viewer for the document created.

    Note: Unlike the OnDownloadComplete event, OnNavigateComplete2 does not occur if the operation is not successful.

    A test with a nonexistent URL revealed that it fires anyway.

    You may also want to consider writing a handler for the OnBeforeNavigate2 event, in case you want to, f.ex. programmatically cancel navigation to a URL

    procedure TForm1.WebBrowser1BeforeNavigate2(ASender: TObject;
      const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData,
      Headers: OleVariant; var Cancel: WordBool);
    begin
      Edit1.Text := URL;
    end;