Solved

Updating URL with TWebBrowser

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

20 Experts available now in Live!

Get 1:1 Help Now