开发者

How to use unescape() function inside JavaScript?

I have a JSP page in which I have JavaScript function that will be called when a link is clicked. Now, when the value reaches the JavaScript function, the apostrophe is encoded.

Example:

Name#039;s

Before # there is &, which originally should be:

Name's

I have used the unescape() decode function, but nothing seems to work. In the end, I had to del开发者_运维问答ete the characters and add the apostrophe. Does anyone know a fix for this? Is it that JSP doesn't support encoding for &? When I was writing the same encode value in this page, it changed the symbol to the apostrophe, which is what I wanted in my code.


Built-in Javascript function such as unescape(), decodeURIComponent() has nothing to do with the string you are working on, because the one you are looking to decode are HTML entites.

There are no HTML entites decoder available in Javascript, but since you are working with a browser, if the string is considered safe, you may do the following (in JQuery, for example)

var str = $('<p />').html(str).text();

It bascially insert the string as HTML to a <p> element and then extract the text within.

Edit: I just realize the JSP output you posted is not real HTML entities; To process the example given you should use the following, add & before every #1234; and make it &#1234;:

var str = $('<p />').html(str.replace(/\#(\d+)\;/g '&#$1;')).text();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜