0

I have jsp code as:

onclick="showURL('${result.url}')"

${result.url} is dynamic value. When an apostrophe comes in the URL I get the error.

I have tried all the methods like escape, encodeURI, replacing the single quotes with double but nothing works.

Script call is as follows:

function showURL(name){
    alert(name);
} 
1

3 Answers 3

1
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
${fn:escapeXml(myString)}

See: How can I escape special HTML characters in JSP?

Sign up to request clarification or add additional context in comments.

1 Comment

I am using it as href. So when fn:escapeXML says as undefined attribute
1

You need to ensure that ${result.url} returns a valid URL. Quotes are invalid in URLs. It sounds like that you're returning an URL with a query string with unencoded parameters like follows

public String getUrl() {
    return "page.jsp?foo=" + foo + "&bar=" + bar;
}

You need to change the method as follows

public String getUrl() {
    return "page.jsp?foo=" + URLEncoder.encode(foo, "UTF-8") + "&bar=" + URLEncoder.encode(bar, "UTF-8");
}

You cannot fix this in the JavaScript side with escape(), etc. It's already too late then.

Comments

0

why not just do this:

onclick=showURL("${result.url}");

function showURL (result_url) {
    alert("<c:out value='"+ result_url + "' />");
}

then you don't have to worry about escaping at all.

-tjw

2 Comments

I have tried above but getting error a attribute name undefined in onclick call. I have used escape once and it worked for all special character except apostrophe.
My code worked when i used c:out value directly in the java script body. var splCharUrl="<c:out value="${result.url}"/>"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.