WebBrowser Get Domain Name Question

Posted on 2009-12-26
Last Modified: 2012-05-08
Hi all,

How can I get the current domain url in a twebbrowser leaving out any filenames please?

For example: If the current url is:

then I need to get the url without the it would leave:

hope you can help


p.s: The url I hold in a TEdit

Question by:ST3VO
    LVL 22

    Accepted Solution

    First of all, use the property LocationUrl.
    Then, to extract the domain, use the attached function.

    //From Delphi7 PosEx is in StrUtils 
    //if you use Delphi6 Use this PoseEx Function, else just add StrUtils to the uses clause
    function PosEx(Substr: string; S: string; P: Integer): Integer;
      if P <= 0 then Result := 0
      else Result := Pos(Substr, Copy(S, P, Length(S)));
      if Result <> 0 then Result := Result + P - 1;
    //This is the Funtion to extract the domain from the url
    //Use it calling YourDomain := GetDomain(YourWebBrowser.LocationUrl);
    Function GetDomain(URL:String):String;
     if Pos('http://',URL) = 0 then result := URL
          result := Copy(URL,1,PosEx('/', URL, 8)-1);
        while (Length(result) > 0) and (result[Length(result)] = '/') do
          Delete(result,Length(result),1); // strip trailing /

    Open in new window


    Author Closing Comment

    Perfect thanks!!! :o)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now