<元字符集=& utf-8& gt;声明自己文件的编码? [英] &lt;meta charset=&quot;utf-8&quot;&gt; declares encoding of own file?
问题描述
我了解到HTML文件必须在head
元素中包含<meta charset="utf-8">
元素,以符合标准.
I read that an HTML file has to contain the <meta charset="utf-8">
element in the head
-element to be standard-conforming.
为什么在文件本身中指定文件的编码有意义?为了读取meta
元素,必须已经知道编码.因此,再次指定编码似乎是多余的/没用的.
Why does it make sense to specifiy the encoding of a file in the file itself? In order to read the meta
-element one has to know the encoding already; so it seems redundant/useless to specify the encoding again.
推荐答案
在读取此元素之前,将使用用户代理的默认编码来解释文档. (通常是ISO-8859-1.)如果编码与默认编码不同,则会根据meta元素重新解释文档.这就是为什么您应尽早将其放置在正文中,或者最好使用HTTP标头(请参见下文).
Until this element is read, the document is interpreted with the default encoding of the user agent. (This is often ISO-8859-1.) If the encoding is different from the default, then the document is re-interpreted according to the meta element. That's why you should place it as early as possible in the body, or preferably use an HTTP header (see below).
使用<meta>
元素的希望是,前面的字符都在ASCII字符集中,几乎可以在所有字符集中正确地解释这些字符.
The hope with the <meta>
element is that the preceding characters are all in the ASCII character set, which are interpreted correctly in just about all character sets.
但是,通常,并且如果可能的话,此信息应在HTTP响应标头中发送:
In general, however, and if it is possible, this information should be sent in an HTTP response header:
Content-Type: text/html; charset=utf-8
这可确保从头开始正确地解释文档.
This ensures that the document is interpreted correctly from the start.
这篇关于<元字符集=& utf-8& gt;声明自己文件的编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!