html 页面指定 Content-Type
经常遇到页面显示文字乱码问题,大多数是由于字符编码问题导致。 通常设置
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
在页面头指定字符charset=utf-8等编码方式。
Always declare the encoding of your document. Use the HTTP header if you can. Always use an in-document declaration too. Setting the character encoding should be done in the Content-Type http header, but can also be set with the attribute. The charset specified in the meta element and http header must match.
查看了大多主流网站首页都将放在标签下第一行,是为了在html页面在解析过程中先指定文档编码方式,防止浏览器自动检测使用何种方式,加快页面查找时间。
In order for all browsers to recognize a declaration, it must be
- Within the element,
- Before any elements that contain text, such as the element, AND
- Within the first 512 bytes of your document, including DOCTYPE and whitespace 百度:
<head>
<meta content="text/html;charset=gb2312" http-equiv="Content-Type">
<title>百度一下,你就知道</title>
</head>
QQ:
<head>
<meta content="text/html;charset=gb2312" http-equiv="Content-Type">
<meta content="IE=EmulateIE7" http-equiv="X-UA-Compatible">
<title>腾讯首页</title>...
</head>
Yahoo
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Yahoo!</title>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
</head>
Taobao
<head>
<meta charset="gbk">
<title>淘宝网 - 淘!我喜欢</title>
</head>
参考: http://code.google.com/intl/zh-CN/speed/page-speed/docs/rendering.html#SpecifyCharsetEarly http://www.cnblogs.com/EWall/archive/2011/04/28/2031744.html http://www.w3.org/International/tutorials/tutorial-char-enc/ http://code.google.com/p/doctype/wiki/MetaCharsetAttribute http://htmlpurifier.org/docs/enduser-utf8.html http://www.cherny.com/webdev/103/the-ie-8-x-ua-compatible-meta-tag