---
title: "html 页面指定 Content-Type"
author: "deng131"
date: "2011-04-21T13:56:00Z"
tags: []
categories: ["Web前端"]
slug: "html-content-type"
draft: false
lastmod: "2011-04-28T16:26:14Z"
id: 1011069
url: "https://thinking80s.iteye.com/blog/1011069"
---

经常遇到页面显示文字乱码问题，大多数是由于字符编码问题导致。
通常设置
```java
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
```

在页面<head>头指定字符charset=utf-8等编码方式。

<span style="color:blue">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 <meta charset> attribute. 
The charset specified in the meta element and http header must match.</span>

查看了大多主流网站首页都将<meta http-equiv="Content-Type" content="text/html; charset=utf-8">放在<meta>标签下第一行，是为了在html页面在解析过程中先指定文档编码方式，防止浏览器自动检测使用何种方式，加快页面查找时间。

<span style="color:darkblue">In order for all browsers to recognize a <meta charset> declaration, it must be

* Within the <head> element,
* Before any elements that contain text, such as the <title> element, AND
* Within the first 512 bytes of your document, including DOCTYPE and whitespace 
</span>
百度：

```java
<head>
	<meta content="text/html;charset=gb2312" http-equiv="Content-Type">
	<title>百度一下，你就知道</title>
</head>
```

QQ：

```java
<head>
	<meta content="text/html;charset=gb2312" http-equiv="Content-Type">
	<meta content="IE=EmulateIE7" http-equiv="X-UA-Compatible">
	<title>腾讯首页</title>...
</head>

```

Yahoo

```java
<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

```java
<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>