Opening TWebBrowser link in default browser
Asked Answered
C

2

6

My application displays a small banner loaded from the web in a TWebBrowser control. This banner is actually a HTML page including an image; when the users click the image it takes them to the promotional campaign we're currently running.

The bad thing here is that when clicking the link in TWebBrowser, the campaign page is opened in Internet Explorer, not in their default browser. I know this happens because TWebBrowser is a IE-based control, but is there a way to open the link in users' browser of choice?

Thank you.

Christianachristiane answered 19/7, 2012 at 8:36 Comment(0)
B
9

In the OnBeforeNavigate2 event, check the requested URL and if it is one that you want to launch then Stop() the current navigation and call ShellExecute() to launch the URL in the user's default external browser.

procedure TForm1.WebBrowser1BeforeNavigate2(Sender: TObject; pDisp: IDispatch; var URL: Variant; var Flags: Variant; var TargetFrameName: Variant; var PostData: Variant; var Headers: Variant; var Cancel: WordBool);
begin  
  if (URL should be launched) then
  begin
    Cancel := True;
    WebBrowser1.Stop;
    ShellExecute(0, nil, PChar(String(Url)), nil, nil, SW_SHOWNORMAL);
  end;
end;
Bloodstream answered 20/7, 2012 at 2:11 Comment(3)
Hey Remy. I don't know the exact URL I want to access and that is why I haven't included the code in the client application. I'm using the banner to announce new products, updates, campaigns, and promotions. I need to be able to update the banner on the server.Christianachristiane
If the TWebBrowser displays your own HTML page with just the image being the only link on the page, then you can cancel+launch every URL that goes through the OnBeforeNavigate2 event. If you need something a little more targetted, you can use the OnDocumentComplete event to access IE's DOM interfaces to assign an OnClick event handler directly to the image's hyperlink element. Your event handler can grab the link's current URL and launch it with ShellExecute() whenever the link is clicked.Bloodstream
How to implement this Firemonkey mobile applications.? And this event was not there for TwebBrowserExcogitate
G
3

TWebBrowser exposes DWebBrowserExents2::NewWindow2 via its own NewWindow2 event

So handle the event and provide the automation interface to the event sender

procedure TForm1.WebBrowser1NewWindow2(
    ASender: TObject; var ppDisp: IDispatch; var Cancel: WordBool);

begin  
// create a new browser (e.g. hosted on a new tab /MDI form/ top level window)
// and expose the browser as a property of the new window. 
// Here a form2 object is created to host the new webbrowser instance
...
form2.InitNavigate=False;//the navigation will be triggered after this event
form2.Visible=False;//new window is only for getting the url
ppDisp := form2.WebBrowser1.Application;  
form2.Show;
end;

Now you can get the the new window's URL in the BeforeNavigate2 event handler on form2. Cancel the event and you can use ShellExecute to launch the default browser.

If you only support Windows SP SP2 or higher, you can hook the NewWindow3 event which provide the URL in the arguments before the new window is created.

Gerlach answered 20/7, 2012 at 0:28 Comment(7)
Another option is to set Cancel=True instead, and then use ShellExecute() to launch the URL in the user's default browser outside of TWebBrowser.Bloodstream
That won't work for links whose target is javascript in some IE versions that treat clicking on a javascript URI as a navigation event. The url in BeforeNavigate2 would be javascript:function name in that case.Lamond
Since you have to know the URL before you can launch it, simply check the URL and cancel only the ones you are going to launch externally.Bloodstream
What I am saying is sometimes the URL is not known at BeforeNavigate2 or even NewWindow2. If you cancel BeforeNavigate2 you have no chance of knowing what the URL would be.Lamond
The BeforeNavigate2 event tells you the URL that is being navigated to. It is a parameter of the event. It even allows you to alter the URL before the navigate continues, if you do not cancel it.Bloodstream
No it does not always tell you the url of the new window, which is what the user is asking, it only tells you the url being navigated to. When the navigation event is triggered by a javascript URL, there is a difference between the url of the new window (usually begins with http:) and the url being navigated to (begins with javascript:)Lamond
You can look at the URL, and if it is a Javascript URL then let the browser handle it normally, and if it is an HTTP URL then cancel the navigation and launch the URL externally.Bloodstream

© 2022 - 2024 — McMap. All rights reserved.