3

I am having a text field for entering a URL. I am using anchor tag on visualforce page to display that field.

<a target="_blank" style="text-decoration:none" href="{!contactInfo.TURL}">--Click here--</a>

When i enter value like "www.google.com" in that field and when i click on that link it renders like "https://c.cs15.visual.force.com/apex/www.google.com".

But when i enter "http://www.google.com" in that field it render correctly.

How can i make it work correctly without enforcing user to enter "http://" preceding the website link.

3

Limited Fix for Question Raised

Yes without the HTTP protocol, the browser will treat it as a relative link to the current page, to implicitly add this you can adjust the expression in your page like this.

<a target="_blank" style="text-decoration:none" href="http://{!contactInfo.TURL}">--Click here--</a>

Alternative Thoughts...

Note that you are making an assumption here on the protocol applied, e.g. http vs https, i personally think its better to clarify to users this is an actual web URL and needs to be valid or preferablly if you can, use the URL field type in the platform, this will adjust the value for the user as you desire.

enter image description here

enter image description here

enter image description here

  • 1
    Good one. That doesn't enforce entering HTTP, but it will still end up like that. – Willem Mulder Jan 23 '14 at 10:50
  • 1
    I added one last option using a formula field that I think should work. I don't have too much experience with Salesforce though, I'm still learning :-) – Willem Mulder Jan 23 '14 at 10:58
  • 1
    Yeah that could work if the poster is not able to change the field type from Text to URL, +1 from my side! :-) – Andrew Fawcett Jan 23 '14 at 11:01
  • 1
    Trying using <apex:outputField>? – Andrew Fawcett Jan 23 '14 at 11:52
  • 1
    There is no way to customise the behaviour of outputField its whole purpose is to provide the native functionality of the field type. Is resorting to coding something to emulate this behaviour (checking the URL etc) really worth it i wonder? Is it perhaps the case the end user wants to see where the link will take them before clicking? Just some thoughts, if you need to emulate the platform feature, take a look at the other answer that will help you down that path. Good luck! :-) – Andrew Fawcett Jan 23 '14 at 16:06
2

Create a getter method on the controller

Like:

public String getCorrectURL()
{ 
    string someURL = '';
    // ... do some logic
    return someURL;
}

And then in your visualforce page, call the setter like this

<apex:outputText value="{!CorrectURL}" />

That should work.

Alternative answer that's not yet mentioned

Create a formula field that adds the http:// part if it's missing, and use that formula field in your visualforce markup.

IF(
    OR(BEGINS(TURL__c, "http://"), BEGINS (TURL__c, "https://")),
    TURL__c, 
    "http://" + TURL__c
)

That way, you can keep the original input and still get correct output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.