How and when to use Html encode

谁都会走 提交于 2019-12-04 12:43:22

The answer came out of thejh's and my discussion in the comment to the question. I was not sure what to mark as answer so I decided to answer my own question. I hope that's ok.

It seems like when setting a value of an attribute (like the textbox's "value") .NET automatically html encodes the value so there is no need to do this by yourself.

When setting a html controls inner HTML though, it's important that you do html encode the value.

Thanks Thejh, sorry I couldn't up vote anything u wrote.

edit: I can't mark this as the answer for another 2 days.

in the case of

<input type="textbox" value="xxx"/>

'xxx' is an attribute, and you should use a different encoding. In ASP.NET it's HtmlAttributeEncode for example.

For HTML attributes, encode backslashes and double quotes.

  1. Replace every \ by \\
  2. Replace every " by \"

Oh, by the way: Sometimes PHP does this for you, see here.

This feature has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!