如何HttpUtility.HtmlDe code在JavaScript [英] How to HttpUtility.HtmlDecode in javascript

查看:149
本文介绍了如何HttpUtility.HtmlDe code在JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个localStorage的拿着一个字符串,它看起来像如下:

I have a localStorage holding a string that looks like the follows:

  "ABC&nbps;&nbps;&nbps;&nbps;ABC Description"

我从localStorage的这个字符串,我需要这个字符串的值设置为一个下拉控件的一个选项,以空格代替无间断空间实体(为了与其他选项保持一致)。

I get this string from the localStorage and I need to set the value of this string into an Option of a drop-down control, replacing the no-break space entity with empty spaces (in order to align with other options).

在ASP.NET背后的code,我会用HttpUtility.HtmlDe code。但是你怎么去code此字符串的JavaScript?

In the code behind of ASP.NET I would use HttpUtility.HtmlDecode. But how do you decode this string in the javascript?

推荐答案

您可以用code以下位转换HTML文本(使用JavaScript的刚)

You can use the following bit of code to convert HTML to text (using just javascript)

 var span = document.createElement("SPAN");
 span.innerHTML = "ABC    ABC Description";
 alert(span.innerText);

它创建了一个虚拟的元素,设置它的HTML到您的HTML并提取文本。

It creates a dummy element, sets its HTML to your HTML and extracts the text.

请注意,它应该是 NBSP (不间断空格),而不是 nbps

Note that it should be nbsp (non-breaking space) and not nbps

这是说,你这样做的方式表明您允许用户输入任意HTML。这不是在安全方面(用户可以放入恶意的HTML在此字段)。

That said, the way you are doing this suggests that you are allowing the user to enter arbitrary HTML. This is not a good idea in terms of security (a user could put in malicious HTML into this field).

这篇关于如何HttpUtility.HtmlDe code在JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆