Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Updating URL with TWebBrowser

Posted on 2003-10-28
4
Medium Priority
?
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

670 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