简体   繁体   中英

Is URL encoding “href” attribute of a anchor element necessary?

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?

URLs can only be sent over the Internet using the ASCII character-set.

Usually, browser does the encoding for you.

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.

It depends on what standard you want your page to conform to:

  • For (X)HTML5, URIs containing non-ASCII characters (ie, 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? .

是的,我用<meta http-equiv="Content-type" content="text/html; charset=windows-1251"/>这个,这意味着俄语和链接只是转<a href="/?????">Persian News</a>所以你需要一个合适的字符集和编码才能使其正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM