The HTMLMetaElement.charset
property is a string that specifies the character encoding used in a document. Using non-UTF-8 character encodings is strongly discouraged as this can create unexpected results on form submission and URL encoding. For more details, see Character encodings in HTML.
The following example queries a <meta>
element that contains a charset
attribute. The charset
value is logged to the console to display the character encoding of the document:
let meta = document.querySelector("meta[charset]");
console.log(meta.attributes["charset"].value);
The following example creates a new <meta>
element with a charset
attribute set to utf-8
and appends it to the document <head>
:
let meta = document.createElement("meta");
meta.charset = "utf-8";
document.head.appendChild(meta);