HTML: <textarea>-Tag: How to correctly escape HTML and JavaScript content displayed in there?

前端 未结 2 1026
盖世英雄少女心
盖世英雄少女心 2020-12-11 14:47

I have a HTML Tag and the $FOO Variable will be filled with arbitrary HTML and JavaScript Content, to be disp

相关标签:
2条回答
  • 2020-12-11 15:38

    You need to replace the special character of HTML with character references (either numerical character references or entity references), in textarea, at least &, < and >.

    0 讨论(0)
  • 2020-12-11 15:43

    I first tought of escaping it HTML

    Yes, that's right. The contents of a <textarea> are no different from the contents of any other element like a <span> or a <p>: if you want to put some text inside you must HTML-escape any < or & characters in it to &lt; and &amp; respectively.

    Browsers do tend to give you more leeway with fault markup in <textarea>​s, in that the fallback for invalid unescaped < symbols is to render them as text instead of tags, but that doesn't make it any less wrong or dangerous (for XSS).

    but this didnt work

    Please post what you did that didn't work. HTML-escaping is definitely the right thing.

    0 讨论(0)
提交回复
热议问题