I am working on Google Calendar API. Whenever I create any event and enter the description of that event having new line, It gives me error like:
“Unterminated String constant”.
I am passing that Event Description from Java to Javascript. So how to format the string having new line?
My code is:
var EventDescription='<% =eventDetails.getEventDescription(eventIndex)%>'
eventDetails -java class
getEventDEscription -Method to get the Description of event.
eventIndex - no of the event whoes Description is needed .
It is working properly otherwise .only giving error when description is having new line.
Try to imagine yourself as the JSP container, then as the JavaScript interpreter. Or simply look at the source code of the generated HTML page.
The line
is interpreted by the JSP container. So
eventDetails.getEventDescription(eventIndex)is executed, and the result of this method call is put in the response. Suppose the result is composed of two lines:So the generated JavaScript code is:
And this is invalid JavaScript code. The correct JavaScript code would be
So, you need to JavaScript-escape the result of the Java method call before putting it in the response. Look at Apache commons-lang StringEscapeUtils.escapeEcmaScript() method.