Solved

Updating URL with TWebBrowser

Posted on 2003-10-28
4
332 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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A short film showing how OnPage and Connectwise integration works.

914 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

21 Experts available now in Live!

Get 1:1 Help Now