English 中文(简体)
如何在JavaScript中使用unescape()函数?
原标题:How to use unescape() function inside JavaScript?

我有一个JSP页面,其中有JavaScript函数,当单击链接时会调用该函数。现在,当值到达JavaScript函数时,就会对撇号进行编码。

示例:

Name#039;s

在#之前有&;,最初应该是:

Name s

我使用了unescape()解码函数,但似乎什么都不起作用。最后,我不得不删除字符并添加撇号。有人知道解决这个问题的办法吗?是JSP不支持对<code>&?当我在这个页面中编写相同的编码值时,它将符号改为撇号,这正是我在代码中想要的。

问题回答

内置的Javascript函数,如unescape()decodeURIComponent()与您正在处理的字符串无关,因为您要解码的是HTML实体。

Javascript中没有可用的HTML实体解码器,但由于您使用的是浏览器,如果字符串被认为是安全的,则可以执行以下操作(例如,在JQuery中)

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

它基本上将字符串作为HTML插入到<;p>元素,然后提取其中的文本。

编辑:我只是意识到您发布的JSP输出不是真正的HTML实体;要处理给定的示例,您应该使用以下内容,添加&并使其成为&#1234

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




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.