JavaScript Document xmlEncoding Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Deprecated
Instance property

What You’ll Learn

Document.xmlEncoding is a deprecated read-only instance property that reported encoding from the XML declaration. Learn when it returns null, MDN’s UTF-16 example, how it differs from characterSet, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

string | null

03

Source

XML decl

04

HTML

Often null

05

Replace

characterSet

06

Status

Deprecated

Introduction

XML files can start with a declaration such as <?xml version="1.0" encoding="UTF-16"?>. That encoding attribute tells parsers which character encoding the bytes use.

MDN: Document.xmlEncoding returned the encoding as determined by the XML declaration. It should be null if unspecified or unknown. MDN warns not to use this attribute—it was removed from DOM Level 4 and dropped in Firefox 10.0.

💡
HTML vs XML

Normal HTML tutorials use <meta charset="utf-8">, not an XML declaration. On those pages, document.xmlEncoding is typically null. Use characterSet instead.

Related Document tutorials: characterSet, contentType, Document constructor.

Understanding Document.xmlEncoding

A read-only instance property from the legacy DOM XML era.

  • Value — encoding string from XML declaration (MDN), e.g. "UTF-16".
  • null — when encoding is unspecified or unknown (MDN).
  • HTML documents — usually null (no XML declaration).
  • Read-only — you cannot assign a new encoding here.
  • Deprecated — removed from DOM Level 4; not in Firefox 10+ (MDN).

📝 Syntax

JavaScript
document.xmlEncoding

Value

A string or null (MDN).

MDN XML declaration example

XML
<?xml version="1.0" encoding="UTF-16"?>
<!-- document.xmlEncoding would be "UTF-16" on legacy XML docs -->

⚡ Quick Reference

GoalCode / note
Read (legacy)document.xmlEncoding
HTML encodingdocument.characterSet
Check nulldocument.xmlEncoding === null
Declare HTML UTF-8<meta charset="utf-8">
MDN XML exampleencoding="UTF-16""UTF-16"
MDN statusDeprecated

🔍 At a Glance

Four facts about document.xmlEncoding.

Type
string|null

Read-only

From
XML decl

Legacy

HTML
null

Typical

Status
deprecated

DOM4 removed

📋 xmlEncoding vs characterSet

document.xmlEncodingdocument.characterSet
Document typeLegacy XML focusHTML & modern DOM
Typical HTML valuenull"UTF-8"
MDN statusDeprecatedBaseline Widely available
Recommended?NoYes

Examples Gallery

Examples follow MDN Document: xmlEncoding. On HTML try-it pages, expect null in modern browsers.

📚 Getting Started

Read the deprecated property and interpret the result.

Example 1 — Read document.xmlEncoding

Log the property on the current document.

JavaScript
console.log(document.xmlEncoding);
// Typical HTML page in modern browsers: null
Try It Yourself

How It Works

Without an XML declaration, MDN says the result should be null.

Example 2 — MDN: UTF-16 from XML Declaration

When a document starts with encoding="UTF-16", legacy browsers could return "UTF-16".

Concept
<?xml version="1.0" encoding="UTF-16"?>
<root/>
JavaScript
// Legacy XML document (MDN example):
console.log(document.xmlEncoding); // "UTF-16"
Try It Yourself

How It Works

This taught how XML declarations map to the property. Do not depend on it today.

📈 Compare, Guard & Modern API

Migrate legacy code to characterSet.

Example 3 — Compare with document.characterSet

On HTML pages, characterSet gives the real encoding label.

JavaScript
console.log({
  xmlEncoding: document.xmlEncoding,
  characterSet: document.characterSet
});
// Typical: { xmlEncoding: null, characterSet: "UTF-8" }
Try It Yourself

How It Works

Use characterSet for encoding diagnostics on HTML pages.

Example 4 — Guard Legacy Code

Feature-detect before reading a removed property.

JavaScript
function getDocumentEncoding() {
  if ("xmlEncoding" in document && document.xmlEncoding !== null) {
    return document.xmlEncoding; // legacy path
  }
  return document.characterSet; // modern path
}

console.log(getDocumentEncoding());
Try It Yourself

How It Works

When migrating old libraries, fall back to characterSet automatically.

Example 5 — Modern: Read characterSet Instead

MDN’s recommended path for HTML document encoding.

JavaScript
const encoding = document.characterSet;
const isUtf8 = encoding.toUpperCase().replace(/_/g, "-") === "UTF-8";

console.log({ encoding, isUtf8 });
Try It Yourself

How It Works

See the characterSet tutorial for full encoding coverage.

🚀 Why This Property Existed

  • Legacy XML documents — read declaration encoding from the DOM (historical).
  • Migration audits — find xmlEncoding reads and replace with characterSet.
  • Old tutorials — understand pre-DOM4 encoding APIs.
  • Not for HTML — use <meta charset> and characterSet.
  • Not for new XML — parse XML with DOMParser or fetch bytes + decode explicitly.
  • Firefox note — removed since Firefox 10.0 (MDN).

🧠 How Encoding Is Determined Today

1

Browser loads bytes

HTML or XML file arrives over the network or from disk.

Load
2

Encoding signals applied

BOM, HTTP Content-Type, or <meta charset> for HTML.

Signals
3

Read characterSet

Modern scripts use the Baseline characterSet property.

Standard
4

Skip xmlEncoding

Deprecated and removed from DOM Level 4 (MDN).

📝 Notes

  • MDN: Deprecated — do not use; removed from DOM Level 4.
  • Firefox: not supported since version 10.0 (MDN warning).
  • Returns null when encoding is unspecified or unknown (MDN).
  • HTML pages: prefer document.characterSet and <meta charset="utf-8">.
  • Related: characterSet, contentType, visibilityState.

Legacy Browser Support

Document.xmlEncoding is deprecated and removed from DOM Level 4. MDN notes it is no longer supported in Firefox 10.0+. Logos use the shared browser-image-sprite.png sprite from this project.

Deprecated · Legacy

Document.xmlEncoding

Removed XML declaration encoding hook — use characterSet for HTML.

Legacy Avoid in new code
Mozilla Firefox Removed since 10.0 (MDN)
No support
Google Chrome May expose null · do not rely
Legacy / limited
Apple Safari Do not rely on xmlEncoding
Legacy / limited
Microsoft Edge Chromium · prefer characterSet
Legacy / limited
Opera Follow Chromium behavior
Legacy / limited
Internet Explorer Legacy DOM XML era
Legacy support
Document.xmlEncoding Deprecated / removed

Bottom line: Do not use document.xmlEncoding. Read document.characterSet on HTML pages and parse XML declarations explicitly when handling XML files.

Conclusion

Document.xmlEncoding was a read-only legacy hook for XML declaration encoding. MDN deprecates it, DOM Level 4 removed it, and modern Firefox dropped it long ago. On HTML pages use document.characterSet instead.

Continue with xmlVersion, characterSet, ownerDocument, contentType, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use document.characterSet for HTML encoding
  • Declare <meta charset="utf-8"> in every HTML page
  • Replace xmlEncoding when updating legacy scripts
  • Parse XML with DOMParser for XML string data
  • Log characterSet when debugging mojibake

❌ Don’t

  • Read document.xmlEncoding in new production code
  • Assume UTF-16 from xmlEncoding on HTML pages
  • Confuse xmlEncoding with contentType
  • Expect support in Firefox 10+ (MDN)
  • Mix XML declarations into normal HTML5 documents

Key Takeaways

Knowledge Unlocked

Five things to remember about xmlEncoding

Deprecated XML declaration encoding — use characterSet.

5
Core concepts
⚠️02

Status

deprecated

DOM4
📄03

HTML

null

Typical
04

Replace

characterSet

Modern
🐦05

Firefox

removed 10+

MDN

❓ 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.
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. For HTML documents use document.characterSet. For XML data, parse with DOMParser or read the declaration from the source — do not rely on document.xmlEncoding.
HTML documents use meta charset and HTTP headers, not an XML declaration. MDN: xmlEncoding reflects the XML declaration encoding, which HTML pages normally do not have.
xmlEncoding reads the legacy XML declaration encoding on XML documents. characterSet returns the encoding label the browser uses to render the page (MDN Baseline API) — the modern choice for HTML.
No. It is read-only. Even when it existed, you could not set the XML declaration encoding through this property.
Did you know?

MDN’s example shows that <?xml version="1.0" encoding="UTF-16"?> would yield "UTF-16" from document.xmlEncoding on legacy XML documents—but the same MDN page warns the property was removed from DOM Level 4 and dropped in Firefox 10.0. Today’s HTML pages should never depend on it.

Next: xmlVersion

Learn the deprecated XML declaration version property and MDN’s HTML detection trick.

xmlVersion →

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.

6 people found this page helpful