Home  >  Article  >  Web Front-end  >  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.

王林
王林forward
2023-08-19 18:05:12652browse

HTML DOM characterSet attribute represents the character set associated with the charset attribute of the e8e496c15ba93d81f6ea4fe5f55a2244 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

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>

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>

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;
}

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete