I have to pass a string value to JavaScript from JSP page.
I am using
display("<%=name%>")
It works fine but when i have string like 'sweet milk'
, JavaScript throws the error
Unterminated string literal
How to solve this?
您的字符串包含单引号-您可以将单引号转义为“ \\ x27”,将双引号转义为“ \\ x22”,然后将其传递给javascript。
You probably have characters in your String that should be escaped in Javascript. For example, if your string is My name is "John"
, your code will generate
var a = "My name is "John"";
which is invalid.
You should use StringEscapeUtils.escapeJavaScript from commons-lang to make sure everything is correctly escaped (single and double quotes, newlines, tabs, etc.).
I guess there's an error in the generated JavaScript code. Is there any way to look at you page? I suggest to look at the generated source code of that page.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.