Question

I know that declaring the charset of a page using the older style, like so:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

will override any declarations of the charset of a page using the newer style, like so:

<meta charset="utf-8" />

but I am wondering what will two of the older-style declarations do? Is the first one taken over the second? Is the second taken over the first? Or are they both omitted and simply excluded from the remainder of the algorithm that determines the actual encoding of the page.


Example

As an example, I am wondering which character encoding declaration in the below snippet will be accepted and factored into the algorithm that determines the page's character encoding:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    ...
</head>


I was unable to find the answer to this in the specs or any other reputable source of documentation (though I could have missed it).


Side-Note

I am not asking this question because of a difficulty with properly setting a page's character encoding, but rather because I am curious about the answer and whether or not browsers prefer the first or second declaration. As such, alternative solutions/workarounds will not be viable answers for this question.

Thanks for your help! Happy coding! :)

Was it helpful?

Solution

If a browser is using meta tags to determine the character encoding, then the first one of such tags takes effect. This has not been specified in current HTML specifications, but it is the common practice and being standardized in HTML5, see HTML5 CR, clause 8.2.2.2 Determining the character encoding.

There is no difference between the two types of meta tags mentioned in the question. They are just syntactic variants. It is the order of appearance that matters: the first one is used. (It is relatively simple to test this, but you need to use a page that does not have HTTP header specifying the encoding and does not contain Byte Order Mark, since such information would override any meta tags.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top