--- title: document.characterSet slug: Web/API/Document/characterSet tags: - API translation_of: Web/API/Document/characterSet ---

{{ ApiRef("DOM") }}

Document.characterSet 只读属性返回当前文档的字符编码。该字符编码是用于渲染此文档的字符集,可能与该页面指定的编码不同。(用户可以重写编码方式。)

document.charset 和document.inputEncoding 属性是document.characterSet 的遗留别名。不要再使用它们。

语法

var string = document.characterSet

示例

<button onclick="alert(document.characterSet);">查看字符集</button>
//返回当前文档的字符集,比如"ISO-8859-1" 或者 "UTF-8"

规范

规范 状态 Comment
{{SpecName('DOM WHATWG', '#dom-document-characterset', 'characterSet')}} {{Spec2('DOM WHATWG')}} 初始定义.

浏览器兼容性

{{CompatibilityTable}}

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
基本支持 {{CompatChrome(45.0)}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} 9 (possibly earlier)
字符集 Made read-only in {{CompatChrome(45)}} {{CompatVersionUnknown}} {{CompatGeckoDesktop(44)}} {{CompatUnknown}} {{CompatUnknown}} 9 (possibly earlier)
Feature Android Android Webview Edge Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
基本支持 {{CompatNo}} {{CompatChrome(45.0)}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatChrome(45.0)}}
字符集 {{CompatUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile(44)}} 2.5 {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}