Solved

Updating URL with TWebBrowser

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

735 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