dsetton
dsetton

Reputation: 826

Setting character-encoding in HTTP headers or the <meta> element, which has precedence?

I am currently reading Dive into HTML5 and I've got a question regarding the character encoding part.

The book starts by saying that the reason there exists a <meta> element used to set the character encoding (like <meta http-equiv="Content-Type" content="text/html; charset=utf-8">)is that not everyone has access to web servers, and hence they are not able to set the HTTP Content-Type header, which would be the correct way to do it. Ok, that makes a lot of sense.

But then he goes on and says that

The HTTP header is the preferred method, and it overrides the <meta> tag if present.

Now, that doesn't make sense. Wasn't the <meta> tag supposed to allow you to override server behavior, in cases when you don't have access to it?

Upvotes: 2

Views: 368

Answers (2)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201558

In very old days, the idea was that servers could look into the contents and pick up information from meta tags. They mostly didn’t, and it was an odd idea really. What happened is that servers pay no attention to such tags (they don’t even read them, as they don’t parse normal HTML documents at all, they just deliver them), but browsers do.

Servers generally use some sort of mapping table that maps file name extensions to media types, possibly with charset parameters, to be used in HTTP headers. When using techniques like PHP, the headers are generated by server-side program code.

This leaves little room for meta tags server-side, but they are used by browsers when servers do not provide encoding information in HTTP headers or when resources are accessed locally. Modern HTML specifications reflect this, and they clearly specify that preference is to be given to HTTP headers, and this is what browsers do.

Upvotes: 1

robertc
robertc

Reputation: 75707

The http-equiv metas allow you to provide a value if the server is not sending it. They are not intended to be replacements for server side configuration, in fact it was always intended the server would parse the (static) file and send the appropriate header. Have a look at this description in the HTML 3.2 spec (Jan 1997):

The HTTP-EQUIV attribute can be used in place of the NAME attribute and has a special significance when documents are retrieved via the Hypertext Transfer Protocol (HTTP). HTTP servers may use the property name specified by the HTTP-EQUIV attribute to create an RFC 822 style header in the HTTP response. This can't be used to set certain HTTP headers though, see the HTTP specification for details.

Upvotes: 3

Related Questions