function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
scottnelsonsmithscottnelsonsmith 

Component Title escaping HTML characters

In my page I have a component:

 

<c:AccountFooSection title="DISPLAYING ALL CURRENT FOOS FOR {!titleLink}" />

In my controller, I have this:

 

private String getTitleLink { 
    return '<br/><a style=\'white-space:nowrap\' href=\'/' + id + '\'>' + name + '</a>';
}

 This is what I see for the component's title: 

 

DISPLAYING ALL CURRENT FOOS FOR <br/><a style='white-space:nowrap' href='/001M0000002Kt7jIAC'>Foo Test</a>

 

I tried placing HTML tags in the title itself, but got the same result. There is no "escape='false'" for components.

 

Any suggestions? Thanks!

 

Note: This actually works fine on Winter '10 production, but not in my Spring '11 sandbox. Is Salesforce rendering differently now?

Best Answer chosen by Admin (Salesforce Developers) 
scottnelsonsmithscottnelsonsmith

You're correct in that component titles are displayed as escaped, so I had to build a workaround with jQuery.

 

Since I need to unescape component titles in various pages, I'll create a static resource called unescapeMainTitle_js for reusability.

 

 

$(document).ready(function() {
$('#unescapeMainTitle .mainTitle').html(unescape($('#unescapeMainTitle .mainTitle').text()));
});

This grabs the innerHTML of the component title, unescapes it, and puts it back (note: works only when there's just one component per page).

 

 

Here's one of my pages:

 

 

<apex:includeScript value="{!$Resource.unescapeMainTitle_js}" />

<div id="unescapeMainTitle">
<c:AccountFooSection title="DISPLAYING ALL CURRENT FOOS FOR {!titleLink}" />
</div>

 Works fine, just in time for the Spring '11 push!

 

All Answers

Jim BoudreauxJim Boudreaux

don't know if this will work, but try replacing the '<' with '&lt;' and '>' with '&gt;'

 

 

scottnelsonsmithscottnelsonsmith

Thanks for the suggestion, but then it prints out:

 

DISPLAYING ALL CURRENT FOOS FOR &lt;br/&gt;&lt;a style='white-space:nowrap' href='/001M0000002Kt7jIAC'&gt;Foo Test&lt;/a&gt;

 

So it seems to never escape anything.

Jim BoudreauxJim Boudreaux

Dang, sorry. I think you will just have to resign yourself to the fact that you cannot put a link in a title.

scottnelsonsmithscottnelsonsmith

You're correct in that component titles are displayed as escaped, so I had to build a workaround with jQuery.

 

Since I need to unescape component titles in various pages, I'll create a static resource called unescapeMainTitle_js for reusability.

 

 

$(document).ready(function() {
$('#unescapeMainTitle .mainTitle').html(unescape($('#unescapeMainTitle .mainTitle').text()));
});

This grabs the innerHTML of the component title, unescapes it, and puts it back (note: works only when there's just one component per page).

 

 

Here's one of my pages:

 

 

<apex:includeScript value="{!$Resource.unescapeMainTitle_js}" />

<div id="unescapeMainTitle">
<c:AccountFooSection title="DISPLAYING ALL CURRENT FOOS FOR {!titleLink}" />
</div>

 Works fine, just in time for the Spring '11 push!

 

This was selected as the best answer