Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
146
rated 0 times [  149] [ 3]  / answers: 1 / hits: 89105  / 15 Years ago, thu, september 24, 2009, 12:00:00

I have a user form. If the user types in a string with ' or as part of it I have no problem. The form is submitted and saved correctly to the database. My problem is when I reload the page (all entries can be modified and are loaded into a list in the JSP before being displayed). On loading the page I get an error saying:



missing ) after argument list 'Caroline's message', n


What do I need to do to escape this string for displaying it on the frontend?



Here is the code I am using on the frontend to read in the data and store it in a JavaScript object. I am not fully sure where I need to escape. The field causing the problem is c.getComName:



communications[<%=i%>][1] = new CommObject('<%=c.getComId()%>', '<%=c.getComName()%>');


UPDATED WITH HTML GENERATED:



communications[0][1] = new CommObject('101', 'Caroline's Message');

More From » java

 Answers
7

Use the Apache StringEscapeUtils.escapeJavaScript function.




Escapes the characters in a String using JavaScript String rules.

Escapes any values it finds into their JavaScript String form.
Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)

So a tab becomes the characters '\' and 't'.


[#98631] Saturday, September 19, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reecep

Total Points: 141
Total Questions: 95
Total Answers: 113

Location: Finland
Member since Mon, Nov 8, 2021
3 Years ago
reecep questions
Wed, Dec 8, 21, 00:00, 2 Years ago
Fri, Jul 30, 21, 00:00, 3 Years ago
Wed, Nov 18, 20, 00:00, 4 Years ago
Sat, Mar 14, 20, 00:00, 4 Years ago
Mon, Feb 3, 20, 00:00, 4 Years ago
;