Search code examples
javascriptxpages

javascript friendly replacement for ' character available?


on my xpage I set a javascript variable onload:

var custName = '#{javascript: matterBean.getMatter().getCustomer().getCustName();}';

this variable I use to replace later the title of a bootstrap dialog.

I notice names with the ' character in it break the code.

I could replace it with

var custName = "#{javascript: matterBean.getMatter().getCustomer().getCustName();}";

but then this would break if the name contains the " character.

Is there an alternative, a javascript friendly replacement so the back-end code (java) could return a javascript friendly replacement for the ' character?


Solution

  • You can use Apache Commons StringEscapeUtils for escaping the string. Example:

    StringEscapeUtils.escapeJavaScript(str);