我have read我应该将我的符号编码为 HTML 中的 &
。
然而,来自受人尊敬的公司的大量代码样本不知何故忘记这样做。
只是我头脑中的几个例子:
Google Web Fonts示例代码:
<link href='http://fonts.googleapis.com/css?family=PT+Sans&subset=latin,cyrillic' rel='stylesheet' type='text/css'>
Google Maps文档:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&language=ja">
Twitter Anywhere官方教程:
<script src="http://platform.twitter.com/anywhere.js?id=YOUR_API_KEY&v=1" type="text/javascript"></script>
不在链接中转义&符号有什么真正的好处吗?
这与浏览器怪癖有关吗?这只是文档中的错误吗?
Dear answerers, please make sure you're answering the right question.
I know I should escape ampersands per spec. I also know why the mechanism was invented in the first place. I'm not asking about this. My question is:
Is there a reason API documentation by respectable companies often violates this rule?
最佳答案
这里有两个不同的上下文。
- 在 javascript href 的上下文中,& 很好,不应该被编码。
- 在 HTML 链接中 & 是被禁止的并且应该被转义。
在 HTML 链接上下文中,在将地址传递给 HTTP 进程之前,将对 HTML 字符实体进行解码; URL 编码的字符不会,因为服务器可以直接读取它。
关于html - 为什么 Twitter 和 Google API 文档不对 URL 中的 & 符号进行编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8874706/