Reputation: 171
I am trying to link an image to a website using the TWebImageControl
and the TWebLinkLabel
components, I am aware that I should use an onClick
event however how would the code be written in order for the link to work?
Upvotes: 2
Views: 176
Reputation: 3242
There are two ways to link to websites or open URLs. One is using window.open
and the other one is using Application.Navigate
.
The Application.Navigate
function takes two parameters. The first parameter is the URL and the second is the TNavigationTarget
.
Navigate to a URL in a new tab:
Application.Navigate('https://www.bing.com', TNavigationTarget.ntBlank);
Navigate to a URL in the same tab:
Application.Navigate('https://www.bing.com', TNavigationTarget.ntPage);
Here's a little function that I wrote to work with window.open
:
procedure OpenURL(URL: String; NewTab: Boolean = True);
begin
if NewTab then
window.open(URL, '_blank')
else
window.open(URL, '_self');
end;
The NewTab
parameter is to specify whether you want to open the link in a new browser tab or in the same tab as your website.
So if you want to navigate to let's say "https://www.bing.com/", then you can write the following code in your onClick
event to call the function:
OpenURL('https://www.bing.com', False);
Upvotes: 2
Reputation: 8331
Based on TWebLinkLabel you only need to specify valid URL link as part of its caption and it automatically changes into hyperlink at runtime.
As for TWebImageControl you can specify the URL from which the image is loaded. But of you would like to open a different image when clicking on TImageControl
then you would need to use OnClick event and provide a custom URL as it was shown in Shaun Roselt's answer
Upvotes: 2