Choosing the encoding (13.2.3)
Before tokenizing, a browser settles on a character encoding:
- A byte order mark at the start:
EF BB BFis UTF-8,FE FFUTF-16 big-endian,FF FEUTF-16 little-endian. It overrides everything else. - The transport: a
charsetparameter on the HTTPContent-Typeheader. (A file opened from disk has none.) - The prescan: the first 1024 bytes are scanned for a
<meta>tag (below). The value is looked up in the Encoding Standard’s list of labels. A label of UTF-16 is treated as UTF-8, because the prescan would not have worked on real UTF-16;x-user-definedbecomes Windows-1252. - Otherwise a guess, typically Windows-1252 for Western locales, or UTF-8 if the bytes are valid UTF-8.
Conforming documents use UTF-8 and declare it with
<meta charset="utf-8">, fully inside the first 1024 bytes.
The prescan
The prescan walks the first 1024 bytes, skipping comments (<!-- … -->)
and any other tags (<x …>, <!…>,
<?…>, and their attributes, read carefully so that a
> in a quoted value does not end the tag early). The first
<meta tag with a usable charset decides:
<meta charset="x">givesx.<meta http-equiv="Content-Type" content="text/html; charset=x">givesx, but only if thehttp-equivattribute is present.
A <meta> inside a comment therefore does not count, which is
the viewer’s reading too.
Media type and extensions
| Media type | text/html, optional charset |
|---|---|
| Extensions | .html, .htm |
| Macintosh file type | TEXT |
| Fragment identifiers | The id of an element, or a name on a; or the text fragment syntax |
| Magic numbers | None. Browsers sniff by looking for tags such as <!DOCTYPE html or <html |
XHTML, the XML form of HTML, uses application/xhtml+xml and an XML parser;
a file served as text/html is parsed as HTML whatever it looks like.