JavaScript Document xmlEncoding Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

Document.xmlEncoding returns the encoding from the XML declaration (for example UTF-16), or null if unspecified or unknown (MDN).

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use document.xmlEncoding. Start with this line.

JavaScript
document.xmlEncoding
CallWhat it does
document.xmlEncodingPrimary signature.

Built into the browser. Read or assign document.xmlEncoding — no import required.

Library declarations
JavaScript
document.xmlEncoding

Parameters

ParameterDescription
SetterOptional. Was read-only
Modern pathRequired. document.characterSet

Return Value

Return ValueDescription
*The current value of document.xmlEncoding.

JavaScript Examples

Example 1 — Feature Detect xmlEncoding

Many modern engines omit this deprecated property entirely.

JavaScript
const supported = "xmlEncoding" in document;
console.log(supported);

if (supported) {
  console.log(document.xmlEncoding);
} else {
  console.log("xmlEncoding not available");
}
Try It Yourself

How It Works

  1. Use in. Safer than reading a removed property.
  2. Expect gaps. Firefox removed it; others may omit it too.
  3. Do not polyfill. There is nothing useful to restore.
  4. Migrate intent. Use characterSet for encoding.

Example 2 — Prefer characterSet (Modern)

The Baseline API for the document’s character encoding.

JavaScript
console.log(document.characterSet);
// e.g. "UTF-8"

console.log("Use characterSet — not xmlEncoding");
Try It Yourself

How It Works

  1. Read characterSet. No arguments — a string encoding label.
  2. Expect a real value on HTML. Typically "UTF-8" for modern pages.
  3. Skip xmlEncoding. That API is deprecated and often missing.
  4. Use one source of truth. Prefer the Baseline Document encoding API.

Example 3 — Safe Legacy Read (Audit Only)

Only for understanding old pages — never for new features.

JavaScript
const legacy =
  "xmlEncoding" in document ? document.xmlEncoding : null;

console.log(legacy); // "UTF-16" | null | missing → null
console.log("Prefer document.characterSet");
Try It Yourself

How It Works

  1. Guard first. Only read when the property exists.
  2. Expect null on HTML. No XML declaration encoding.
  3. Treat as archaeology. Useful for audits, not product logic.
  4. Point forward. Log a reminder to use characterSet.

Browser Support

Document.xmlEncoding is Deprecated (MDN). It was removed from DOM Level 4 and is not supported in Firefox 10+. Do not rely on it.

✓ Deprecated · Avoid

Document.xmlEncoding

Legacy XML declaration encoding — removed from the modern DOM path.

Legacy Avoid in new apps
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full support
xmlEncoding Deprecated

Bottom line: Safe only as historical knowledge. For document encoding, use characterSet; for MIME type, use contentType.

Frequently Asked Questions

A read-only string with the encoding from the XML declaration (for example "UTF-16"), or null if unspecified or unknown (MDN). On typical HTML pages it is usually null or the property is missing.
Yes. MDN marks Document.xmlEncoding as Deprecated. It was removed from the DOM Level 4 specification and is no longer supported in Firefox 10.0 and later.
No. Prefer document.characterSet for the encoding the browser uses, and document.contentType for the MIME type. Do not rely on xmlEncoding.
HTML documents use meta charset and HTTP headers, not an XML declaration. xmlEncoding reflects the XML declaration encoding, which HTML pages normally do not have (MDN).
xmlEncoding is a legacy XML-declaration field. characterSet is the modern Baseline API that returns the encoding label the browser uses for the document.
No. It was read-only. Even when supported, you could not set the XML declaration encoding through this property.

Next: xmlVersion

Learn the sibling deprecated XML declaration version property.

xmlVersion tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful