HTML-encoding lost when attribute read from input field

前端 未结 25 3817
时光说笑
时光说笑 2020-11-21 04:04

I’m using JavaScript to pull a value out from a hidden field and display it in a textbox. The value in the hidden field is encoded.

For example,



        
25条回答
  •  悲&欢浪女
    2020-11-21 04:47

    Here is a simple javascript solution. It extends String object with a method "HTMLEncode" which can be used on an object without parameter, or with a parameter.

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

    I have made a gist "HTMLEncode method for javascript".

提交回复
热议问题