需要对 HTML 链接中的字符进行编码吗?
我应该对 url 中包含的字符进行编码吗?
示例:
<a href="http://google.com?topicId=1&pageId=1">Some link using &</a>
或
<a href="http://google.com?topicId=1&pageId=1">Some link using &</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。
在HTML中(据我所知包括XHTML和HTML5),所有属性值和标签内容应该编码:
Yes.
In HTML (including XHTML and HTML5, as far as I know), all attribute values and tag content should be encoded:
Web 编程中有两种不同的编码用于不同的目的,并且很容易混淆。
要显示为 HTML 的文本中的特殊字符需要编码为 HTML 实体。特别是诸如“<”之类的字符它们是 HTML 标记的一部分,但如果对要使用的字符编码有任何疑问,它也可能对其他特殊字符有用。
URL 中的特殊字符需要进行 URL 编码(用 %nn 代码替换)。
如果接收者将 HTML 实体视为 HTML 文本,那么将 HTML 实体放入 URL 中并没有什么坏处;但如果它是程序指令的一部分(例如用于分隔 CGI 查询字符串中的参数的 &),则不应对其进行编码。
There are two different kinds of encoding which are needed for different purposes in web programming, and it is easy to get confused.
Special characters in text which is to be displayed as HTML need to be encoded as HTML entities. This is particularly characters such as '<' which are part of HTML markup, but it may also be useful for other special characters if there is any doubt about the character encoding to be used.
Special characters in a URL need to be URL-encoded (replaced by %nn codes).
There is no harm in putting an HTML entity into a URL if it is going to be treated as HTML text by whatever receives it; but if it is part of an instruction to a program (such as the & used to separate arguments in a CGI query string) you should not encode it.
取决于您的文件的提供和识别方式。
对于 XHTML,是的,而且是必需的。
对于 HTML 来说,不行,这样做也是不正确的。
Depends how your files are being served up and identified.
For XHTML, yes and it's required.
For HTML, no and it's incorrect to do it.