/Web APIs

HTMLMetaElement: charset property

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.


A string.


Reading character encoding from a meta element

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:

// given <meta charset="utf-8">
let meta = document.querySelector("meta[charset]");
// "utf-8"

Creating a meta element

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

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.