Max_Salah
Max_Salah

Reputation: 2497

Escape Quote - javascript, struts 2

I read some struts2 variable in javascript as follows:

<javascript type="text/javascript"> 
var data='<s:property value="simulationInfos"/>';
<javascript>

If my simulationInfos contains single quote ', I get the error : unexpected identifier.

therefore, I tried to escape the quote as follows:

var data='<s:property value="simInfos" escapeJavaScript="true"/>';
and var data='<s:property value="simInfos" escapeHTML="true"/>';

I get the error: Attribute escapeJavaScript (or escapeHTML) invalid for tag property according to TLD.

Any Idea?

Upvotes: 0

Views: 4456

Answers (2)

tusar
tusar

Reputation: 3424

If you want to use the inbuilt escapeJavascript of <s:property>, then upgrade to 2.2.1 Also in JavaScript, you can easily avoid unexpected identifier error if you had used double quotes.

var data = "<s:property value="simulationInfos"/>";

Upvotes: 2

FlavorScape
FlavorScape

Reputation: 14299

Where does the single quote appear? In the value, I'm assuming?

In that case, in your javascript before you perform the struts2 operation, do run this code on the value. This is a regular expression to remove quotations for javascript.

 var escapedString = valueString.replace(/(['"])/g, "\\$1"); //note, includes double quotes

If you need to keep the quotes as URL encoded, do this

 var escapedString = valueString.replace(/(['])/g, "&apos;");

Upvotes: 1

Related Questions