You are here: Reference > JavaScript > client-side > HTML DOM > properties > charset (document, link, meta, ...)

charset property (document, link, meta, ...)

Browser support:
Sets or retrieves the character encoding of the document, a linked document or a script block.
If you want to specify the character encoding of the document:
in HTML5, use the charset property of the meta element,
in other document types, use the httpEquiv property with the value of 'content-type' and content property of the meta element.
For details, please see Example 1 and 2 and the page for the doctype element.
Note that the browser support of the charset property in JavaScript and the browser support of the charset attribute in HTML are different. While the charset attribute is supported by all commonly used browsers, the charset property is only supported in Internet Explorer.
To retrieve the character encoding of the document at runtime, use the charset and characterSet properties.
To get the character set that belongs to the current regional and language settings of the operating system, use the defaultCharset property. Finally, use the inputEncoding property to get the character encoding used for parsing the document.
See Example 3 for details.

Syntax:

object.charset;
You can find the related objects in the Supported by objects section below.
This property is read/write.
HTML page for this property: charset

Possible values:

Sets or retrieves the character set. For more information, see the page for character sets.
Default: this property has no default value.

Example HTML code 1:

This example shows how to set the character encoding of the document in HTML5:
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
</head>
<body>
</body>
</html>
Did you find this example helpful? yes no

Example HTML code 2:

This example shows how to set the character encoding of the document in document types other than HTML5:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
</body>
</html>
Did you find this example helpful? yes no

Example HTML code 3:

This example illustrates the use of the charset attribute:
<script type="text/javascript" src="example.js" charset="utf-8"></script>
Did you find this example helpful? yes no

Example HTML code 4:

This example illustrates the use of the charset property:
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script type="text/javascript">
        function GetCharSet () {
            var message = "";

            var docCharSet = document.characterSet ? document.characterSet : document.charset;
            message += "The character encoding of the document: <b>" + docCharSet + "</b><br/>";

            if (document.defaultCharset) {
                message += "The character encoding belongs to the operating system:  <b>" + document.defaultCharset + "</b><br/>";
            }
            if (document.inputEncoding) {
                message += "The character encoding used for parsing the document:  <b>" + document.inputEncoding + "</b><br/>";
            }

            var info = document.getElementById ("info");
            info.innerHTML = message;
        }
    </script>
</head>
<body onload="GetCharSet ()">
    <div id="info" style="width:500px; background-color:#e0d0d0;"></div>
</body>
</html>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content