duminică, 10 mai 2015

[OmniFaces utilities 2.1/2.0] Escape the given string according the JavaScript code rules


This utility method exist in OmniFaces 2.0, but it was improved in OmniFaces 2.1.

[OmniFaces utilities] The escapeJS() method escapes the given string according the JavaScript code rules. This escapes among others the special characters, the whitespace, the quotes and the unicode characters. Useful whenever you want to use a Java string variable as a JavaScript string variable.

Method:
Usage:

import org.omnifaces.util.Utils;
...
String stringToEscape = "...";
String escapedJS = Utils.escapeJS(stringToEscape, false);

OmniFaces uses this method in the utilities from JSON class.

Niciun comentariu:

Trimiteți un comentariu