Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unterminated string constant

My Description contains an apstrophe('). How to escape it.

<a href='javascript:select("<%= pageBean.replace(list.getColumn(0), "'", "'") %>",
"<%= pageBean.replace(list.getColumn(1), "'", "'") %>");' title="<%=selRpt%>">
<span class='img-view'></span></a>

"<%= pageBean.replace(list.getColumn(1), "'", "'") %>" is the description part in my JSP Scriptlet which contains apstrophe(')

My HTML View

    <a href='javascript:select("JWCCA5",
"Worker's Compensation Form -  California Form 5020(New)");' 
title="Select Report"><span class='img-view'></span></a>
like image 539
John Avatar asked Oct 20 '10 11:10

John


People also ask

What is unterminated string?

This JavaScript error unterminated string literal occurs if there is string which is not terminated properly. String literals must be enclosed by single (') or double (“) quotes.

Is string a literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.


3 Answers

For reserved HTML characters you should use HTML entities. An apostrophe is then reprecented as &#39;:

<a href='javascript:select(
  "<%= pageBean.replace(list.getColumn(0), "'", "&#39;") %>", 
  "<%= pageBean.replace(list.getColumn(1), "'", "&#39;") %>");' title="<%=selRpt%>"> 
<span class='img-view'></span></a>
like image 159
Kdeveloper Avatar answered Oct 16 '22 14:10

Kdeveloper


Use \'

Inside a HTML tag, you need to turn the string into HTML entities, so the quote becomes &#039;

Inside pure JavaScript, you could also escape the quote with a \'

like image 43
Pekka Avatar answered Oct 16 '22 13:10

Pekka


Usually \' should work, but it seems that sometimes you need to use '' (double apostrophe).

Try this one:

<%= pageBean.replace(list.getColumn(0), "'", "\'" %>

or:

<%= pageBean.replace(list.getColumn(0), "'", "''"

One of them should work (from my experience).

For attributes within HTML tags, I would use " (quotation mark) rather than ' (apostrophe).

like image 30
Paweł Dyda Avatar answered Oct 16 '22 14:10

Paweł Dyda