Search code examples
delphirandomwebdelphi-xe6

Navigate to a random website using the TWebBrowser Component in Delphi


I wish to navigate to a random website in the TWebBrowser component. There are millions of websites online, but how do I select a random website and navigate to it in the webbrowser.

I'm planning to make an application which is very basic. One of the buttons is a random website button which takes the user to a random website.

I'm using Delphi XE6. How do I do this?


Solution

  • So after 5 years I have finally decided to answer my own question properly. Here is a function I wrote to navigate to a random website on the internet:

    procedure RandomWebsite;
    const
      A = 'qwertyuioplkjhgfdsazxcvbnm';
    var
      i: Integer;
      s: String;
    begin
      s := '';
      for i := 1 to random(20) do
        s := s + A[random(A.Length)];
      s := s + '.com';
    
      try
        IdIcmpClient1.Host := s;
        IdIcmpClient1.Ping();
        Webbrowser1.Navigate(s);
      except
        RandomWebsite;
      end;
    end;
    

    I'm using the TIdIcmpClient component from INDY to ping the host and see if the website exists. If the website exists, then I navigate to it. If it doesn't exist, then I try a different host url.

    My written procedure is very simple. You can ofcourse modify it to fit your needs and make it even more "random". My example only goes to .com domains and with letters in it.

    This example should be enough however for most people looking for this solution on how to navigate to a random website using Delphi TWebBrowser.