This month kicks off our new “WaSP Asks the W3C” Question and Answer project. In this project, frequently asked questions posed to WaSP by Web authors and designers regarding standards are submitted by WaSP members to the W3C’s Quality Assurance Group for information. The answers are published and archived both here and on the W3C Web Standards Education list, where follow-up discussion also takes place. Signup details can be found at the end of this article.
There are several ways of specifying the character encoding for a particular document. Which of the following methods (or combination thereof) does the W3C recommend, and why?
meta
elementThese three ways of providing the character encoding of a document are not equivalent. When trying to figure out the character encoding of a resource, user agents will try, in this order:
Content-Type
header sent by the servermeta
elementSince the HTTP Content-Type
header has precedence, and is also the easiest
information to retrieve (user-agents do not have to parse the resource to get it), it is almost
always the preferred way to provide the character encoding for an (X)HTML document.
However, in at least two cases, this is simply not possible:
Content-Type
headerIn these cases, an HTML document should provide the character encoding via a
meta
element, and an XML document can provide it via the XML
declaration. If the XML document uses one of the default encodings (UTF-8 or UTF-16) no declaration is needed to manage the
character encoding.
meta
element (and/or the XML declaration for XHTML
documents)Example of an XHTML 1.0 document written in French with an ISO-8859-1 encoding:
Example of an HTML 4.01 document written in French with a UTF-8 encoding:
On the popular Apache Web server, the HTTP Content-Type
header for a resource
can be set up in the .htaccess file, as follows:
This would force the file example.html to be served as ISO-8859-1 even if the server had a different global configuration.
WaSP and W3C member Tim Bray commented on this answer and said:
“If you know that the document you’re sending is going to get read by an XML processor, the server should get the charset right. If the server makes any mistake the rules say that the processor is supposed to do the wrong thing! On the other hand, if the document is going to any kind of HTML reader, the server can usefully try to help and do what is suggested here. So it turns out that it matters whether you serve it as html or xhtml+xml.”
How to serve HTML and XHTML will be discussed in the next issue of WaSP Asks the W3C.
For clarification and discussion on this topic, please address your comments and questions to the W3C Web Standards Education list.
To subscribe to the list, send an email to [email protected] with “Subject: subscribe”. You can read archived posts at http://lists.w3.org/Archives/Public/public-evangelist/.