HTML-encoding lost when attribute read from input field

前端 未结 25 3976
时光说笑
时光说笑 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条回答
  •  猫巷女王i
    2020-11-21 04:49

    Picking what escapeHTML() is doing in the prototype.js

    Adding this script helps you escapeHTML:

    String.prototype.escapeHTML = function() { 
        return this.replace(/&/g,'&').replace(//g,'>')
    }
    

    now you can call escapeHTML method on strings in your script, like:

    var escapedString = "

    this is HTML

    ".escapeHTML(); // gives: "<h1>this is HTML</h1>"

    Hope it helps anyone looking for a simple solution without having to include the entire prototype.js

提交回复
热议问题