HTML DOM characterSet Property


The HTML DOM characterSet Property represents the character set that is associated with the charset attribute of the <meta> element. By default the character set for an HTML document is UTF-8.

The characterSet property returns the character encoding for the HTML document in a string format. The user can override the default characterSet for a web page using the charset property in HTML or the DOM characterSet property.

Syntax

Following is the syntax for characterSet property −

document.characterSet

Example

Let us see an example of the HTML DOM characterSet property −

<!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 −

On clicking the CHECK ENCODE button −

In the above example −

We have first created a button CHECK ENCODE which will execute the encode() function when clicked by the user −

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

The encode() method will get the character encoding of the document using the characterSet property of the document and assigns it to variable x. The encoding is then displayed in the paragraph element with id “Sample” using the innerHTML() method on the paragraph and assigning some text and the appended variable x to it −

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

Updated on: 07-Aug-2019

23 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements