|
MetaCharsetAttribute
The meta charset attribute specifies the character encoding of the current document.
You are here: Home > HTML Reference > Elements > <meta> element > charset attribute UsageThe <meta charset> attribute sets the character encoding of an HTML document. It is extremely important that you explicitly declare the encoding of every HTML document; failing to do so will cause browsers to guess the encoding, which can lead to cross-site scripting attacks if they guess incorrectly. In order for all browsers to recognize a <meta charset> declaration, it must be
Example: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>My home page</title> </head> <body> ... </body> </html> This is equivalent to the the http-equiv attribute with a value of "Content-Type", like this: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> You may use either; there is no need to use both. Both forms are supported by all major browsers. Unfortunately, there is no cross-browser way to retrieve what character encoding the browser ended up choosing (i.e. whether the <meta charset> declaration worked). In non-IE browsers, you can use document.characterSet. In IE, you might think you could document.getElementsByTagName('meta')[0].charset, but this only returns the character encoding you specified, not the encoding that IE is actually using. ValuesIn theory, any HTML document can specify any character encoding. In practice, browsers ship with a limited set of encodings that they support (see links below). HTML 5 states: "Authors should not use JIS_X0212-1990, x-JIS0208, and encodings based on EBCDIC. Authors should not use UTF-32. Authors must not use the CESU-8, UTF-7, BOCU-1 and SCSU encodings. ... Authors are encouraged to use UTF-8." ElementsYou can use the charset attribute on the following elements: Browser compatibility
Further reading |
Sign in to add a comment
The document.characterSet test is sent as text/plain.