Am trying to read a URL address something like


from the Embedded Web Browser then I want to be able to get the url parameters of a link with in my delphi application, so am able to do a messagve box like:

ShowMessage(id); // this would show 563
ShowMessage(name); // this would show mark

anyone got any ideas?

Thanks

Dani AI

Generated

The URL in 's example puts id=563 before the ?, which is non-standard (typical form is ...?id=563&name=mark). That aside, a robust approach is to capture the navigation event from the embedded browser, read the full URL there, and parse both the query string and any non-standard path pieces. The Delphi WebBrowser event to use is OnBeforeNavigate2 (the URL parameter is an OleVariant that can be converted to a string).

A compact, self-contained example follows: it reads the URL in OnBeforeNavigate2, parses key=value pairs from the query, decodes percent-encoding, and also looks for a /id=... pattern when id isn’t in the query.

procedure TForm1.WebBrowserBeforeNavigate2(ASender: TObject; const pDisp: IDispatch;
  var URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool);
var
  sURL, sQuery, idVal, nameVal: string;
  Params: TStringList;
  i, p: Integer;
  pair, key, val: string;
begin
  sURL := VarToStr(URL);

  // get query portion
  p := Pos('?', sURL);
  if p > 0 then sQuery := Copy(sURL, p + 1, MaxInt) else sQuery := '';

  Params := TStringList.Create;
  try
    Params.StrictDelimiter := True;
    Params.Delimiter := '&';
    Params.DelimitedText := sQuery;

    for i := 0 to Params.Count - 1 do
    begin
      pair := Params[i];
      if Pos('=', pair) > 0 then
      begin
        key := LowerCase(UrlDecode(Copy(pair, 1, Pos('=', pair)-1)));
        val := UrlDecode(Copy(pair, Pos('=', pair)+1, MaxInt));
        if key = 'id' then idVal := val
        else if key = 'name' then nameVal := val;
      end;
    end;
  finally
    Params.Free;
  end;

  // fallback: handle URLs like "/id=563?name=..."
  if idVal = '' then idVal := ExtractAfter(sURL, '/id=');
  if idVal = '' then idVal := ExtractAfter(sURL, '?id=');

  if idVal <> '' then ShowMessage('id = ' + idVal);
  if nameVal <> '' then ShowMessage('name = ' + nameVal);
end;

(The helper functions UrlDecode and ExtractAfter are simple routines: UrlDecode converts + to space and %HH sequences to characters; ExtractAfter finds a key and reads until ?, &, / or #.)

Notes: validate and sanitize parameter values (e.g. TryStrToInt for numeric ids). OnBeforeNavigate2 fires before redirects; if the final URL after redirects is needed, inspect OnNavigateComplete2. 's StackOverflow link points to similar parsing ideas; this reply provides a self-contained Delphi-friendly implementation and extra handling for non-standard paths.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.