HTML5 standards learning-detailed coding explanation

黄舟
Release: 2017-03-21 15:14:15
Original
1558 people have browsed it

I believe that every front-end engineer has encountered the "garbled code" brother at one time or another. No matter how solid your foundation is, you will inevitably have a few cups of tea with the "garbled code" brother occasionally during the production process. . As a front-end engineer, how do you specify the encoding of a page? Do you know how the browser recognizes the encoding?

First of all, a very simple example, use Yujian's HTML page to see the differences in various browsers:


        
Copy after login

The simplest HTML,# Both ## andhave no content, and the server does not give a specific encoding statement. Open it directly from the local computer and check the encoding of the page in each browser:

It can be seen from theTablethat each browser has different parsing for pages that do not use any means to declare encoding. Of course, in the simplest page, no matter what encoding is used (of course, the premise is a superset of ASCII), it has no impact, but it is enough to show the importance of setting the encoding correctly.

Encoding Statement

HTML4 andHTML5each adopt a chapter to explain the encoding statement method. You can click here to view the relevant chapters of HTML4 or click here to view the relevant chapters of HTML5. chapter.

First of all, what is coding? Encoding is to specify the browser (or user agent) to use a special algorithm to parse the byte stream in a certain way to obtain the truly correct content. In the HTML standard, encodings can be represented using aliases. Encoding aliases come from the IANA definition, and only encodings that appear in this list can be recognized by browsers. Therefore, if UTF-8 is written as UTF8, the browser may completely ignore it. In addition, encoding aliases arecase-insensitive.

In HTML4, there are three methods to specify the encoding of the page. According to the priority, they are:

  1. The Content-Type field in the HTTP header is followed by characters set.

  2. Use thetag to declare.

  3. For some external resources, such as js files loaded by the