Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding of XHTML and & (ampersand)

People also ask

Which encoding is best for HTML?

Your Best Option: UTF-8 UTF-8 stands for Unicode Transformation Format 8-bit and has held the title of the most popular HTML character encoding since 2008. By 2019, more than 90 percent of all websites use UTF-8. It is also recommended to use as the default HTML character encoding by the World Web Consortium.

Is HTML a UTF-8?

Unicode enables processing, storage, and transport of text independent of platform and language. The default character encoding in HTML-5 is UTF-8.

What is encoding XML?

XML Encoding is defined as the process of converting Unicode characters into binary format and in XML when the processor reads the document it mandatorily encodes the statement to the declared type of encodings, the character encodings are specified through the attribute 'encoding'.

Why UTF-8 is used in HTML?

Why use UTF-8? An HTML page can only be in one encoding. You cannot encode different parts of a document in different encodings. A Unicode-based encoding such as UTF-8 can support many languages and can accommodate pages and forms in any mixture of those languages.


I have just tried this. What you attempted to do is correct. In HTML if you are writing a link the & characters should be encoded as &amp; You would only encode the & as %26 if you wanted a parameter value to contain an ampersand. I just wrote a simple HTML page that contained a link: <a href="Default2.aspx?param1=63&amp;param2=hel">Click me</a> and it worked fine: default2.aspx received the parameters intended and the source passed validation.

The encoding of & as &amp; is required in HTML, not in the link. When the browser sees the &amp; in the HTML source for a link it will interpret it as an ampersand and the link target will be as intended. If you paste a URL into your browser address bar it does not expect it to be HTML and does not try to interpret any HTML encoding that it may contain. This is why your example links that you suggest we should copy/paste into a browser don't work and why we wouldn't expect them to work.

If you post a bit more of your actual code we might be able to see what you have done wrong, but you appear to be heading the right direction by using &amp; in your anchor tags.


It was my fault: the hyperlink control already encoded &, so my URL http://foo?x=1&amp;y=2 was encoded to http://foo?x=1&amp;amp;y=2

Normally the &amp inside the URL is correctly handled by browsers, as you stated.


You could use &amp; instead of & in your URL within your page.

That should allow it to be validated as strict XHTML...

<a href="http://www.example.org/page.aspx?x=1&amp;y=2">Foo</a>

Note, if used by an ASP.NET Request.QueryString function, the query string doesn't use XML encoding; it uses URL encoding:

/mypath/mypage?b=%26stuff

So you need to provide a function translating '&' into %26.

Note: in that case, Server.URLEncode(”neetu & geetu”), which would produce neetu+%26+geetu, is not what you want, since you need to translate & into %26, not just '&'. You must add a replace() call applied to URLEncode result, in order to replace '%26amp;' by '%26'.