1
votes

I have a string in my Visualforce code like so:

String strGood = '';
strGood = item.getGoodPercentage().toPlainString();

I would like to javascript encode the string. I understand that there is the JSENCODE method that can be used on the page itself, but is there an equivalent that can be used in the Visualforce class? If not what is the best approach?

2

2 Answers

3
votes

The JSENCODE function is available in Visualforce. Check out http://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_functions.htm.

If you have the variable jsString in your controller, you can use the following to set the string to a JS variable with the corresponding escaping:

var myVar = {!JSENCODE(jsString)};
2
votes

I presume you mean you want to URL encode this string. There is an Apex function called EncodingUtil that you can use for this purpose.