How HTML headers Declare document types, languages, and character sets

Source: Internet
Author: User
Tags character set header version root directory

At the top of each of your pages, you need a document statement. Yes, I must.

If you do not specify a document type, your HTML is not legitimate HTML, and most browsers use "quirks mode" to process the page, which means the browser thinks you don't know what to do and processes your code in the browser's own way. You can be an HTML master, there are no rivals on Earth, or your HTML can be flawless, CSS can be perfect, but without document statements, or incorrect document statements, your Web page is very hard to pile up with a short-sighted, one-eyed gibbon baby.

The XHTML 1.0 Strict (Strict) document declaration is as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >

The following is a document declaration for XHTML 1.1, which looks more perfect as the latest version of XHTML, but there are still some questions, and then we'll explain a little bit ...

<! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML 1.1//en" "Http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >

If you don't want to give up HTML 4 or you have Netscape 4 die-hard users, you can use XHTML 1.0 Transitional (transition type):

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

The only reason you use this is that you have to be compatible with older, less-used browsers. The transitional XHTML 1.0 allows HTML 4 to be a performance element and may perform better in browsers such as Netscape 4. But using these elements will be detrimental to the efficiency and usability of your Web pages.

Finally, if you're one of the geeks using the framework, you can use the XHTML 1.0 Frameset (framework) document type declarations like the following:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 frameset//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd" >

Note that the DOCTYPE tag must be uppercase and forward with an English half-width exclamation point!. It is the only label that breaks the rules and it does not need to be closed.

  Language declaration

You must specify a primary language for the document, even if the HTTP header or the Xml:lang attribute is set within the HTML start tag. Although it is not necessary to process a legitimate XHTML document, it is also a usability consideration. Values are abbreviated, such as En (Chinese, English), FR (French, French), De (German, German) or MG (Malagasy, what is this?). The translator also does not know, hehe. --translator note).

Declare a document that is primarily in English, as exemplified by the following:

After declaring the main language, you can also use the Xml:lang attribute (such as <span xml:lang= "de" >html hund</span>) in inline if you need to use another language.

  Content Type

The media type and font set of the HTML document may be specified and can be done using HTTP headers, such as:

content-type:text/html; Charset=utf-8

The first part of the HTTP header (such as text/html) is a file MIME type that allows the browser to know how to handle the file's media type. All files have MIME types. JPEG images are image/jpeg,css files that are commonly used text/html text/csss and HTML.

The second part of the HTTP header, such as the UTF-8 part, is the character set.
Perhaps the easiest way to set up an HTTP header is to use the "HTTP synonym (http-equivalent)" header tag in HTML, like this:

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

A bit more complicated is a better way to use the server-side scripting language to send headers. In PHP, you can do this:

? Header ("content-type:text/html; charset= UTF-8 ");?>

If you are unwilling (or unable) to use the server-side scripting language, you may be able to set a ". htaccess" file directly to the server. Most servers (Apache compatible) can use a small text file of ". htaccess" in the root directory and write the following, so you can associate all the "HTML" suffix files with the MIME type and character set:

AddType text/html;charset=utf-8 HTML

The character set includes most Western Latin based "iso-8859-1", Japanese "Shift_JIS", Chinese "GB18030" and UTF-8, a Unicode transformation format version, Provides a single character for a wide range of languages. Basically, you should use a character set that you know can be clearly understood for your users. Unless you use a Latin based language (including English) (Iso-8859-1 is generally accepted), you should use UTF-8 because it can display most characters in most languages, and it is also safe to use because it can be used on large computers.

  Attention

XHTML should be used as a application/xhtml+xml MIME type, and it is clear that this is an XML program. Unfortunately, most browsers don't have a first clue about this. Therefore, it is generally considered nice to use the MIME type of text/html. Based on the recommendations of the Consortium and the future highlights of the Web Standard project, the seasoned XHTML 1.0 may be used for text/html, but XHTML 1.1 should not, which is the site with XHTML 1.0 Strict (strictly) as an example, assuming text/ The MIME type of the HTML. But you can still (or should not) set the correct MIME type for them to the browser, a slight call to the server side.

This site uses PHP to set the Application/xhtml+xml MIME type for XHTML 1.1 to those browsers that can understand and handle this type (such as Mozilla) and set the XHTML 1.0 Strict text/ HTML to other browsers, such as IE. Add the following code to the top of each page:

? if (Stristr ($_server["http_accept"], "Application/xhtml+xml")) {header ("content-type:application/xhtml+xml; Charset=utf-8 "); Echo (' <! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML 1.1//en" "" ">http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd" > "); else {header ("content-type:text/html; Charset=utf-8 "); Echo (' <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "" >http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd ">" ); }?>

These checks verify that the browser accepts Application/xhtml+xml MIME types and, if so, sends the MIME type and writes the XHTML 1.1 class type to HTML. If this MIME type is not accepted, the MIME type of the text/html is sent and the XHTML 1.0 Strict (Strict) document type is written to HTML.

Apart from the peace of mind that you are doing the right thing and preparing for your future path, the most immediate benefit is that, using this method, Mozilla browsers treat your file as an XML program and if your XHTML is not scratched, that means that Mozilla will not work. Then you can make the wrong arrangement without having to run your document with a validator.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.