Unlimited string constant

My Description contains apstrophe ('). How to avoid this.

<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 part of the description in my JSP Scriptlet that contains apstrophe (')

My HTML view

  <a href='javascript:select("JWCCA5", "Worker Compensation Form - California Form 5020(New)");' title="Select Report"><span class='img-view'></span></a> 
+6
java javascript html jsp
source share
7 answers

For reserved HTML characters, you must use HTML objects . Then the apostrophe appears 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> 
+6
source share

Use \'

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

Inside pure JavaScript, you can also escape quotes with \'

+6
source share

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

Try the following:

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

or

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

One of them should work (in my experience).

For attributes in HTML tags, I would use "(quotation mark), not" (apostrophe).

+2
source share

Call a function from HTML and put your JavaScript in this function. It will get by with your problem, but I think it is a little better anyway.

+1
source share

Maybe you could use a Unicode character code? (\ U0027)

+1
source share

You must replace ' with #39; before displaying it.
You can do it in
- properties file, where it comes from the site - in code in ASP

By the way, what are you trying on this line?

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

Can

 "<%= pageBean.replace(list.getColumn(1), "'", "&#39;") %>" 

must do the job.

+1
source share

A typical JSP developer will abandon old-fashioned scripts and use JSTL c:out or fn:escapeXml . Both escapes are predefined XML objects , such as ' to &#39; etc.

Here is an example with fn:escapeXml :

 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> ... <a href="javascript:select('${fn:escapeXml(list.columns[0])}', '${fn:escapeXml(list.columns[1])}');" title="${title}"> 

You may need to change the model to be a more complete Jawabay.

+1
source share

All Articles