Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Updating URL with TWebBrowser

Posted on 2003-10-28
4
Medium Priority
?
348 Views
Last Modified: 2010-04-05
Hi,

I have a TWebBrowser on a form along with a TEdit to specify the URL.  This is used to view both web pages and local pages.  I am trying to get the URL to update in the TEdit when the user navigates to a new page in *exactly* the same way that IE normally does.  I have tried a couple of ways, but neither is entirely successful.

Here are the 2 ways I've tried so far:

1)

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

This causes problems for sites that contains frames because it appears to update the URL for *every* frame, rather than always displaying the URL of the main frame as IE itself would normally do.

For example, if I navigate to www.hotbot.com, the URL gets updated with:

http://www.hotbot.com/
http://hb.lycos.com/header?LHS=1&VID=1651&LHM=0&LHIV=1

However, if www.hotbot.com were entered into IE, the URL would update to just http://www.hotbot.com/, which I would like my program to do too.

2)

I've tried the following:

procedure TForm1.WebBrowser1NavigateComplete2(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
  Edit1.Text := WebBrowser1.LocationURL;
end;

This solved the frames problem but introduces a new problem that the above solution doesn't have!  If I enter a local page, it updates the URL in UNC format, rather than displaying it as typed (which IE itself would normally do).

For example, if I navigate to C:\Test\Test.html, the URL would update to file:///C:/Test/Test.html, rather than simply leaving it at C:\Test\Test.html, which IE itself would do and what I'd like my program to do.

I've spent a lot of time on this but have not been able to come up with a solution.  I would just like the URL to be updated in the same way that IE itself normally does, regarding frames and UNC.

Thanks in Advance,

Richard
0
Comment
Question by:Richard2000
4 Comments
 
LVL 3

Accepted Solution

by:
BJThomsen earned 400 total points
ID: 9635658
Simply Check the URL if the first 4 characters = file and massage it to the version you would like.
0
 

Author Comment

by:Richard2000
ID: 9673932
Hi,

Thank you for you comment.  I've considered your approach, however, I've created this alternative way that seems to work well...

In the form, add LastLocationURL: string; as a private declaration and implement NavigateComplete2 as follows...

procedure TForm1.WebBrowserNavigateComplete2(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
  // Update the address box only if required.  Note that NavigateComplete2
  // appears to fire for *each* frame and so if a page has frames, only the URL
  // of the first (top-level) frame is used to update the address (the other
  // frames are ignored).  URL is used to provide the updated address for the
  // address box rather than LocationURL because LocationURL sometimes converts
  // local URLs to UNC format when URL doesn't.  LocationURL is only used to
  // determine if the address that the user is viewing has changed.  This makes
  // the behaviour consistent with IE itself.

  if WebBrowser1.LocationURL <> LastLocationURL then
  begin
    Edit1.Text := URL;
    LastLocationURL := WebBrowser1.LocationURL;
  end;
end;

Richard
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

927 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