Home >Web Front-end >H5 Tutorial >HTML5 actual combat and analysis of character set attributes (charset and defaultCharset)
HTML5 has also updated the character set attributes, among which the charset and defaultCharset attributes are the newly added character set attributes in HTML5. The detailed explanation of the charset and defaultCharset attributes can be found in HTML5 Practical Combat and Analysis of Character Set Attributes. Let’s learn about them together below.
The charset attribute in HTML5 represents the document medieval The character set used, can also be used to specify a new character set. By default, the value of the charset attribute is "UTF-16", but it can be modified through the meta tag, response header, or directly setting the charset attribute. A small example is as follows:
##
alert(document.charset) // 谷歌下测试 "UTF-8" document.charset = "UTF-16" alert(document.charset) // 谷歌下测试 "UTF-16LE"
## The defaultCharset attribute represents the default browser and operating system Settings, what should be the default character set of the current document. If the document does not have a default character set, the values of the charset attribute and the defaultCharset attribute may be different. A small example is as follows:
JavaScript code
if(document.charset != document.defaultCharset){ alert("编码不一样哟") }
With the charset attribute and defaultCharset attribute, we can get specific information about the character encoding used in the document, and can also reasonably control the character encoding. When this works properly, it can ensure that users can view pages or use applications normally. Browsers that support the document.charset attribute include Safari, Chrome, IE, Firefox and Opera. Browsers that support the document.defaultCharset attribute are Chrome, IE and Safari.
##