我使用JavaScript从隐藏字段中拉出一个值并在文本框中显示它。隐藏字段中的值被编码。

例如,

<input id='hiddenId' type='hidden' value='chalk &amp; cheese' />

被卷入

<input type='text' value='chalk &amp; cheese' />

通过一些jQuery来获取隐藏字段的值(在这一点上,我失去了编码):

$('#hiddenId').attr('value')

问题是当我读粉笔&cheese从隐藏字段,JavaScript似乎失去了编码。我不希望价值是粉笔和奶酪。我想要字面上的amp;被保留。

是否有JavaScript库或jQuery方法可以对字符串进行html编码?


当前回答

这是一个模拟服务器的程序。HTMLEncode函数来自微软的ASP,用纯JavaScript编写:

htmlEncode函数{ var ntable = { “l”:“amp,” “50%”:“莉莉。托姆琳”, “>”:“gt”, “\“”:“参与的。” 出于美观; s=s.replace(/[&<>")/ g, function (ch){ 返回"&"+ntable[ch]+";"; 出于美观) s = s.replace(/[胡言乱语])/g, function(ch) 返回"&#"+ch.charcodeat(0).tostring()+";"; 出于美观); s return; 出于美观

结果不编码撇号,而是编码其他HTML特殊字符和0x20-0x7e范围之外的任何字符。

其他回答

这是一个简单的javascript解决方案。它通过“HTMLEncode”方法扩展String对象,该方法可以用于不带参数的对象,也可以用于带参数的对象。

String.prototype.HTMLEncode = function(str) {
  var result = "";
  var str = (arguments.length===1) ? str : this;
  for(var i=0; i<str.length; i++) {
     var chrcode = str.charCodeAt(i);
     result+=(chrcode>128) ? "&#"+chrcode+";" : str.substr(i,1)
   }
   return result;
}
// TEST
console.log("stetaewteaw æø".HTMLEncode());
console.log("stetaewteaw æø".HTMLEncode("æåøåæå"))

我已经做了一个主旨“HTMLEncode方法javascript”。

下面是一个非jQuery版本,它比jQuery .html()版本和.replace()版本都快得多。这保留了所有空格,但与jQuery版本一样,不处理引号。

function htmlEncode( html ) {
    return document.createElement( 'a' ).appendChild( 
        document.createTextNode( html ) ).parentNode.innerHTML;
};

速度:http://jsperf.com/htmlencoderegex/17

演示:

输出:

脚本:

function htmlEncode( html ) {
    return document.createElement( 'a' ).appendChild( 
        document.createTextNode( html ) ).parentNode.innerHTML;
};

function htmlDecode( html ) {
    var a = document.createElement( 'a' ); a.innerHTML = html;
    return a.textContent;
};

document.getElementById( 'text' ).value = htmlEncode( document.getElementById( 'hidden' ).value );

//sanity check
var html = '<div>   &amp; hello</div>';
document.getElementById( 'same' ).textContent = 
      'html === htmlDecode( htmlEncode( html ) ): ' 
    + ( html === htmlDecode( htmlEncode( html ) ) );

HTML:

<input id="hidden" type="hidden" value="chalk    &amp; cheese" />
<input id="text" value="" />
<div id="same"></div>

我的pure-JS函数:

/**
 * HTML entities encode
 *
 * @param {string} str Input text
 * @return {string} Filtered text
 */
function htmlencode (str){

  var div = document.createElement('div');
  div.appendChild(document.createTextNode(str));
  return div.innerHTML;
}

JavaScript HTML实体编码和解码

这是一个模拟服务器的程序。HTMLEncode函数来自微软的ASP,用纯JavaScript编写:

htmlEncode函数{ var ntable = { “l”:“amp,” “50%”:“莉莉。托姆琳”, “>”:“gt”, “\“”:“参与的。” 出于美观; s=s.replace(/[&<>")/ g, function (ch){ 返回"&"+ntable[ch]+";"; 出于美观) s = s.replace(/[胡言乱语])/g, function(ch) 返回"&#"+ch.charcodeat(0).tostring()+";"; 出于美观); s return; 出于美观

结果不编码撇号,而是编码其他HTML特殊字符和0x20-0x7e范围之外的任何字符。

<script>
String.prototype.htmlEncode = function () {
    return String(this)
        .replace(/&/g, '&amp;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#39;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;');

}

var aString = '<script>alert("I hack your site")</script>';
console.log(aString.htmlEncode());
</script>

将输出:&lt;script&gt;alert(&quot;I hack your site&quot;)&lt;/script&gt;

. htmlencode()一旦定义,就可以在所有字符串上访问。