Saeed Neamati
Saeed Neamati

Reputation: 35822

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?

Upvotes: 5

Views: 6433

Answers (4)

lxgr
lxgr

Reputation: 3797

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?.

Upvotes: 3

hoymkot
hoymkot

Reputation: 458

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

Usually, browser does the encoding for you.

Upvotes: 5

Fadli Saad
Fadli Saad

Reputation: 222

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.

Upvotes: 2

dwalldorf
dwalldorf

Reputation: 1379

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.

Upvotes: 3

Related Questions