cancel
Showing results for 
Search instead for 
Did you mean: 

Show URL's in string as hyperlink

vishal_gupta15
Contributor
0 Kudos

Hi All,

We are getting a getting a String with multiple URLs ( different url based on the back end logic). I am using XML view , where I have tried the Text and Label to show the String.

In the controller I am using following code to convert the URL as hyperlink

resString = tempString.replace(/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim, '<a href="$1" target="_blank">$1</a>');

and after this I am using

this.getView().byId("__text").setText(resString );

But it appear as the simple text.

Kindly suggest.

_vishal

Accepted Solutions (1)

Accepted Solutions (1)

maheshpalavalli
Active Contributor
0 Kudos

There is an UI5 control 'sap.m.Link' which can be used for this scenario. I hope it is not the case where you are using m.text instead of m.link for some reason.

If it is the case then to change text to link, you need to get the html and do it as i think sap.m.text wont allow us to change the format of text(If i remember). see the link below for ref.

https://archive.sap.com/discussions/thread/3854797

BR,

Mahesh

vishal_gupta15
Contributor
0 Kudos

Hi Mahesh,

Thanks for response.

We do not want to makes the entire string as the hyperlink.

let us assume, If we are getting following string from the back end

tempString = You can search at google . https://www.google.com. OR you can also search at https://help.sap.com.

then the the requirement is make the hyperlinks for URL section only, rest text should appear as it is.

_vishal

maheshpalavalli
Active Contributor
0 Kudos

Ok then refer to my second part of the answer on how to solve ur issue..

BR,

Mahesh

vishal_gupta15
Contributor

the link shared ( discussion thread) help to resolve the issue.

Thanks

_vishal

Answers (1)

Answers (1)

former_member560141
Participant
0 Kudos

You can change the text to link. something like this:

<m:Link id="link"/>

resString = tempString.replace(/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim, '$1'); this.getView().byId("link").setHref(resString);
this.getView().byId("link").setText(resString);
vishal_gupta15
Contributor
0 Kudos

Hi Nicholas,

Thanks for response.

It makes the entire string as the hyperlink.

If we are getting following string from the back end

tempString = You can search at google . https://www.google.com. OR you can also search at https://help.sap.com.

then the this solution is making entire tempString as hyperlink. But the requirement is make the hyperlinks for URL section only.

rest text should appear as it is.

_vishal