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.
The syntax of the characterSet attribute is as follows:
document.characterSet
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>
This will produce the following output−
Click the CHECK ENCODE button−
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!