URL 编码为“href”吗?锚元素的属性是否必要?
我检查了网页的 HTML 代码,并使用 Firefox 的 HTML 验证器插件,我看到它抱怨链接的 href
属性,该属性包含未进行 URL 编码的 Unicode 字符。
当前的 URL 是:
<a href='/اخبار'>Persian News</a>
然而,验证器希望它是:
<a href='/%D8%A7%D8%AE%D8%A8%D8%A7%D8%B1'>Persian News</>
我已经在几乎所有浏览器中测试了此链接(甚至回到 IE6)。它工作得很好。那么,这里有什么问题呢?我为什么要对其进行编码?验证器是否已过时?如果 标记的
href
属性内的 Unicode 字符未进行 URL 编码,我可能会遇到什么问题?
I checked the HTML code of my webpage and validated it using Firefox's HTML Validator add-on, and I saw that it complaints about the href
attribute of a link, which contains Unicode characters, which are not URL encoded.
The current URL is:
<a href='/اخبار'>Persian News</a>
However, the validator wants it to be:
<a href='/%D8%A7%D8%AE%D8%A8%D8%A7%D8%B1'>Persian News</>
I've tested this link in almost every browser (even back to IE6). It works just fine. So, what is the problem here? Why should I encode it? Is validator out of date? What problem may I encounter of not URL encoding Unicode characters inside the href
attribute of an <a>
tag?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
URL 只能使用 ASCII 字符集通过 Internet 发送。
通常,浏览器会为您进行编码。
URLs can only be sent over the Internet using the ASCII character-set.
Usually, browser does the encoding for you.
不支持该语言(编码)的浏览器将无法打开该 URL。您应该对其进行编码,以确保每个人都能够使用您网站的所有功能。
Browsers that do not support this language (encoding) will not be able to open the URL. You should encode it to make sure that everybody is able to use all functionality of your website.
这取决于您希望页面符合什么标准:
对于 (X)HTML5,只要您的文档采用 UTF-8 或 UTF- 编码,包含非 ASCII 字符(即 IRI)的 URI 就是有效的。 16 并相应地发送 MIME 标头。
在 HTML4/XHTML1 文档中,所有非 ASCII 字符始终必须转义。
另请参阅IRI 作为 HTML 属性值是否有效? 的答案。
It depends on what standard you want your page to conform to:
For (X)HTML5, URIs containing non-ASCII characters (i.e., IRIs) are valid, as long as your document is encoded in UTF-8 or UTF-16 and the MIME headers are sent accordingly.
In HTML4/XHTML1 documents, all non-ASCII characters always have to be escaped.
See also the answer to Are IRIs valid as HTML attribute values?.
是的,我已经测试过了
这对俄语来说意味着链接就转了
波斯新闻
所以你需要一个正确的字符集和编码才能使其正常工作。
Yes, I've test this with
<meta http-equiv="Content-type" content="text/html; charset=windows-1251"/>
which meant for Russian and the link just turn
<a href="/?????">Persian News</a>
so you need a proper charset and encoding to make it works fine.