Chaibi Alaa
Chaibi Alaa

Reputation: 1386

Ajax.oncomplete escaping the singlequote incorrectly

I'm using the Omnifaces ajax.oncomplete function to show a toastr message in my JSF 2 page. The problem I'm facing is that I'm developping a frensh web application and we use a lot of quotes ('). When I add the quote, the browser throws a malformed XML exception :

malformedXML: missing ) after argument list

While I'm using the braqueted slash to make it ignore the quote and treat it as a string :

Ajax.oncomplete("toastr.warning('Vérifier l\'adresse e-mail saisie.')");

Is there a way to pass this exception ?

Upvotes: 0

Views: 134

Answers (1)

BalusC
BalusC

Reputation: 1109222

The \ is also an escape character in Java itself. So, ultimately the \ got stripped off by Java.

You need to double-escape it to represent a literal \, so it arrives as a real \ in JavaScript.

Ajax.oncomplete("toastr.warning('Vérifier l\\'adresse e-mail saisie.')");

Alternatively, if those strings are not hardcoded and thus coming from a dynamic source, and you'd basically thus need to perform automatic escaping, then better use Ajax#data() to automatically let OmniFaces encode the Java variable as a properly formatted JSON object available via OmniFaces.Ajax.data in JavaScript context.

Ajax.data("message", "Vérifier l'adresse e-mail saisie.");
Ajax.oncomplete("toastr.warning(OmniFaces.Ajax.data.message)");

This way you don't need to worry about escaping fuss.

Upvotes: 1

Related Questions