JS中 HTMLEncode和HTMLDecode

本文深入探讨了JavaScript中HTML编码与解码的实现方式,并通过实例展示了如何在前后台交互中应用这些技巧,确保网页内容的安全与正确显示。

<!--js伪编码解码-->
<script language="javascript" type="text/javascript">
function HTMLEncode(html)
{
var temp = document.createElement ("div");
(temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
var output = temp.innerHTML;
temp = null;
return output;
}
function HTMLDecode(text)
{
var temp = document.createElement("div");
temp.innerHTML = text;
var output = temp.innerText || temp.textContent;
temp = null;
return output;
}
</script>

JS 中并非 C#中的Server.HtmlDecode Server.HTMLEncode 他并非真的编码解码 而是伪装:把你要写入的内容放入它自己创建的DIV 中,

例:

$("wordDescription").innerText=HTMLDecode(HTMLDecode(JianJie.toString()));

本人用的是:span

<span id="wordDescription" style="vertical-align:top;"> </span>

////////////////后台:

//string JianJie = Server.HtmlDecode(ds.Tables[0].Rows[0]["gagahjt"].ToString()).ToString();//如在后台html解码 前台需要一个HTMLDecode,否则需要2个HTMLDecode
string JianJie = ds.Tables[0].Rows[0]["gagahjt"].ToString();

转载于:https://www.cnblogs.com/AaronYang/p/3529365.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值