简体   繁体   中英

Escaping single quote creating unrequired “\” in the HTML page

I have to show the name of an employee on an HTML page and on mouse over the employee name again, as shown below.

在此处输入图片说明

There is a problem when employee name contains single quote in innerHTML value which is throwing the JavaScript error

missing ) after argument list
At line: 1

Using StringEscapeUtils.escapeJavaScript solves the JavaScript error but the actual HTML renderer retains the "\\" before the single quote. How do I get rid of it?

You simply can't use the same text in HTML and in JavaScript. They are different languages with different escaping needs.

Use StringEscapeUtils.escapeHtml() for HTML and StringEscapeUtils.escapeJavaScript() for JavaScript.

Edit : yes, show us the code that's generating the HTML. Perhaps you don't need to escape the text for HTML yourself if it's being done by someone else's code.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM