JavaScript Document contentType Property

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

What You’ll Learn

Document.contentType is a read-only instance property that returns the MIME type the document is being rendered as (often text/html). Learn where that value comes from, how it differs from characterSet, why <meta http-equiv> does not change it, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

MIME string

03

Typical

text/html

04

Source

HTTP / MIME

05

vs

characterSet

06

Status

Baseline widely

Introduction

Every web resource has a MIME type that tells the browser what kind of content it is: HTML, XML, SVG, PDF, and so on. For a normal web page, that type is usually text/html.

document.contentType exposes the MIME type the browser is using to render the current document. MDN notes that the value may come from HTTP headers or other MIME sources, and that browser or extension conversions can affect it.

💡
Not controlled by meta tags

MDN: contentType is unaffected by <meta> elements. A <meta http-equiv="Content-Type"> tag will not change this property—set the real HTTP Content-Type header instead.

Related Document tutorials: characterSet, compatMode, Document constructor.

Understanding Document.contentType

A read-only instance property on Document. Its value is a string naming the MIME type used for rendering.

  • Value — MIME type string (e.g. text/html).
  • Read-only — you cannot assign a new MIME type from JavaScript.
  • Source — HTTP headers / MIME sniffing / conversions (MDN).
  • Not from meta<meta> tags do not change it (MDN).
  • Different from encoding — use characterSet for UTF-8 style labels.

📝 Syntax

JavaScript
document.contentType

Value

A read-only string — the MIME type the document is rendered as.

Typical HTML page

JavaScript
console.log(document.contentType);
// "text/html" on most web pages

⚡ Quick Reference

GoalCode / note
Read MIME typedocument.contentType
Check HTML pagedocument.contentType === "text/html"
Check XML-ishdocument.contentType.includes("xml")
Change MIME typeSet HTTP Content-Type (not JS)
Encoding insteaddocument.characterSet
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.contentType.

Type
string

MIME label

Access
read-only

No setter

Typical
text/html

Web pages

Status
baseline

Standard API

📋 Common document MIME types

MIME typeUsually meansNotes
text/htmlHTML documentMost websites
application/xhtml+xmlXHTML / XML-parsed HTMLStricter parsing
application/xml / text/xmlXML documentNot a normal web page
image/svg+xmlSVG as documentOpened SVG files

Examples Gallery

Examples follow MDN Document: contentType. Use View Output or Try It Yourself for each case.

📚 Getting Started

Read the rendered document MIME type.

Example 1 — Log document.contentType

Print the MIME type string for the current document.

JavaScript
console.log(document.contentType);
// "text/html" on a normal HTML page
Try It Yourself

How It Works

The browser reports the MIME type it is using to render this document tree.

Example 2 — Confirm an HTML Document

Branch when the page is rendered as HTML.

JavaScript
const isHtml = document.contentType === "text/html";
console.log("Is HTML document:", isHtml);
console.log("contentType:", document.contentType);
Try It Yourself

How It Works

Useful in shared libraries that may run inside HTML pages or XML/SVG documents.

📈 Encoding, Meta & Diagnostics

Contrast related APIs and build a small debug helper.

Example 3 — contentType vs characterSet

MIME type and encoding answer different questions.

JavaScript
console.log("MIME type:", document.contentType);
console.log("Encoding:", document.characterSet);
// contentType ≈ "what kind?"
// characterSet ≈ "which encoding?"
Try It Yourself

How It Works

See also characterSet for encoding details.

Example 4 — Meta Tags Do Not Change It (MDN)

Even with a Content-Type meta tag, contentType stays from MIME sources.

JavaScript
// HTML may include:
// <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

const meta = document.querySelector('meta[http-equiv="Content-Type" i]');
console.log("meta present:", !!meta);
console.log("document.contentType:", document.contentType);
// MDN: contentType is unaffected by <meta> elements
Try It Yourself

How It Works

Prefer <meta charset="utf-8"> for encoding and a real HTTP header for media type.

Example 5 — Document Type Debug Panel

Bundle MIME, encoding, and mode for support logs.

JavaScript
const info = {
  contentType: document.contentType,
  characterSet: document.characterSet,
  compatMode: document.compatMode,
  isHtml: document.contentType === "text/html"
};

console.log(JSON.stringify(info, null, 2));
Try It Yourself

How It Works

A compact snapshot for bug reports: media type, encoding, and rendering mode together.

🚀 Common Use Cases

  • Shared libraries — detect HTML vs XML/SVG document hosts.
  • Support tooling — log MIME type in diagnostic bundles.
  • Teaching MIME types — show what the browser thinks the page is.
  • Server audits — compare expected HTTP Content-Type with what scripts see.
  • Not for setting type — configure the server header instead of JavaScript.
  • Pair with characterSet — separate “what” from “how encoded.”

🧠 How contentType Is Determined

1

Resource is fetched

Server may send a Content-Type header.

HTTP
2

Browser resolves MIME

Headers, sniffing, and possible conversions apply (MDN).

MIME
3

Document is rendered

HTML, XML, SVG, etc. parse according to that type.

Render
4

Read with contentType

JavaScript exposes the MIME type as a read-only string.

📝 Notes

  • MDN: Baseline Widely available (since April 2018) — no Deprecated / Experimental / Non-standard banner.
  • Read-only — set the correct HTTP Content-Type on the server.
  • Unaffected by <meta> elements (MDN).
  • May be influenced by browser/extension type conversions (MDN).
  • Do not confuse with characterSet (encoding) or compatMode (quirks).
  • Related: characterSet, compatMode, Document constructor.

Universal Browser Support

Document.contentType is marked Baseline Widely available on MDN (since April 2018). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.contentType

Read-only MIME type string for the rendered document — usually text/html on websites.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Not available in legacy IE
No support
Document.contentType Excellent

Bottom line: Use document.contentType to read the document MIME type. Configure Content-Type on the server — meta tags do not change this property.

Conclusion

Document.contentType is the standard read-only way to learn which MIME type the browser is using to render the document. On everyday sites that is almost always text/html. Set media types with HTTP headers—not meta tags or JavaScript assignment.

Continue with cookie, characterSet, compatMode, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Serve HTML with Content-Type: text/html; charset=utf-8
  • Use contentType to detect HTML vs XML/SVG hosts
  • Log MIME type beside characterSet in diagnostics
  • Prefer <meta charset="utf-8"> for encoding declarations
  • Verify server headers when a page is mis-parsed

❌ Don’t

  • Try to assign document.contentType
  • Expect <meta http-equiv="Content-Type"> to change it
  • Confuse MIME type with character encoding
  • Assume every document is text/html without checking
  • Use contentType as a substitute for feature detection

Key Takeaways

Knowledge Unlocked

Five things to remember about document.contentType

Read-only MIME type — usually text/html on websites.

5
Core concepts
02

Status

baseline

Standard
🔒03

Access

read-only

DOM
⚠️04

Meta

no effect

MDN
🌐05

vs

characterSet

Encoding

❓ Frequently Asked Questions

A read-only string with the MIME type the document is being rendered as — for example text/html for a normal HTML page, or application/xml / image/svg+xml for other document types.
No. MDN marks Document.contentType as Baseline Widely available (since April 2018). It is a standard read-only Document property.
MDN: it may come from HTTP headers or other MIME sources, and can be affected by automatic type conversions from the browser or extensions.
No. MDN notes that Document.contentType is unaffected by <meta> elements, including http-equiv content-type declarations.
contentType is the document MIME type (what kind of document). characterSet is the character encoding label (how bytes map to text), such as UTF-8.
No. It is read-only. Set the correct Content-Type HTTP header (or serve the correct file type) on the server instead.
Did you know?

Opening an .svg file directly in the browser often creates a document whose contentType is image/svg+xml instead of text/html. That is one reason scripts written only for HTML pages can behave differently inside SVG documents.

Next: cookie

Learn how to read and write cookies with document.cookie.

cookie →

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