I store raw HTML code in a rich text field in the backend. I query it out in the controller and try to render it as HTML in a VF page. It displays the whole HTML code, it dosent render itself as HTML. I have used escape = false in the output text. This was working properly when the field was a long text area. Please provide me a way to get it to 'escape' once again.
1 Answer
You can re-render the html code with JavaScript as shown in the example below. Rich_Text__c
is a RichText field containing raw html code. Through jQuery the span tag is replaced by a div element with the raw html code created by jQuery. Now you can see rendered html in page.
VF Page
<apex:page controller="RichTextController" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<span id="inputtxt"/>
<script type="text/javascript">
$("#inputtxt").html($('<div/>').html("{!myText}").text());
</script>
</apex:page>
Controller
public class RichTextController {
public String myText {get;set;}
public RichTextController(){
Book__c abook = [SELECT Rich_Text__c FROM Book__c where Id = 'a0Ii000000gKbw5'];
myText = abook.Rich_Text__c;
}
}
-
1
-
@MohithShrivastava Thanks for your comment, I have updated my answer. Feb 1, 2016 at 14:09
-
1This won't pass either.You have to HTML and JSENCODE your getter merge field Feb 1, 2016 at 14:18
-
Of course it can pass the Security Review. Rich Text fields are safe to render unescaped. Oct 24, 2016 at 2:34
-
<apex:outputField>
?