Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do browsers handle <meta> tag that specifies the character-encoding?

Suppose a browser encounters a <meta> tag that specifies the character-encoding, like this:

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

Does it start over from the beginning parsing the page again, since some of the preceding characters in the <head> section may have been interpreted incorrectly? Or are there some other constraints that prevent prior characters from being interpreted incorrectly?

like image 561
Joel Lee Avatar asked Mar 28 '11 14:03

Joel Lee


1 Answers

As far as I know, browsers wont go back after finding a charset declaration in the <head> and they assume a ASCII compatible charset up to that point. Unfortunately I can't find a reference to confirm this.

Confirming browsers will ignore a Content-Type meta element, if the server already provides a Content-Type HTTP header, so you can't override a "wrong" server-side charset with a <meta> element.

The point for the <meta> charset declaration is for HTML documents that are not server by a HTTP server.

That means you shouldn't rely on a <meta> charset declaration in the HTML file, but configure your HTTP server to provide the correct charset. If for some reason you have to rely on a <meta> charset declaration, you should only have ASCII characters up to that point and position it as early in the <head> as possible, preferably as the first element.

like image 106
RoToRa Avatar answered Sep 25 '22 11:09

RoToRa