Home > Web Front-end > HTML Tutorial > HTML DOM characterSet attribute The HTML DOM characterSet property returns the character encoding set of the current document.

HTML DOM characterSet attribute The HTML DOM characterSet property returns the character encoding set of the current document.

王林
Release: 2023-08-19 18:05:12
forward
727 people have browsed it

HTML DOM characterSet attribute represents the character set associated with the charset attribute of the element. By default, the character set of HTML documents is UTF-8.

The characterSet property returns the character encoding of the HTML document in string format. Users can override the default character set of a web page using the charset attribute in HTML or the DOM characterSet attribute.

Syntax

The syntax of the characterSet attribute is as follows:

document.characterSet
Copy after login

Example

Let us look at an example of the HTML DOM characterSet attribute−

<!DOCTYPE html>
<html>
<body>
<p>Click the below button to know the encoding of this HTML document</p>
<button onclick="encode()">CHECK ENCODE</button>
<p id="Sample"></p>
<script>
   function encode() {
      var x = document.characterSet;
      document.getElementById("Sample").innerHTML = "The character encoding used is "+ x;
   }
</script>
</body>
</html>
Copy after login

Output

This will produce the following output−

HTML DOM characterSet 属性

HTML DOM characterSet 属性返回当前文档的字符编码集

Click the CHECK ENCODE button−

HTML DOM characterSet 属性

HTML DOM characterSet 属性返回当前文档的字符编码集

In the above example −

We first create a button CHECK ENCODE, which will execute the encode() function when the user clicks it −

<button onclick="encode()">CHECK ENCODE</button>
Copy after login

encode() method will use the characterSet attribute of the document to obtain The character encoding of the document and assigns it to variable x. Then, use the innerHTML() method of the paragraph element to display the encoding in the paragraph element with the id "Sample" and assign some text and the attached variable x to it −

function encode() {
   var x = document.characterSet;
   document.getElementById("Sample").innerHTML = "The character encoding used is "+ x;
}
Copy after login

The above is the detailed content of HTML DOM characterSet attribute The HTML DOM characterSet property returns the character encoding set of the current document.. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template