JavaScript Document contentType Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

Document.contentType is a read-only string: the MIME type the document is being rendered as (MDN)—for example text/html on a normal HTML page.

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

Syntax

Use document.contentType. Start with this line.

JavaScript
document.contentType
CallWhat it does
document.contentTypePrimary signature.

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

Library declarations
JavaScript
document.contentType

Parameters

ParameterDescription
contentTypeRequired. Read-only MIME type string
characterSetOptional. Encoding label (different question)

Return Value

Return ValueDescription
*The current value of document.contentType.

JavaScript Examples

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

  1. Read the property. No arguments—just document.contentType.
  2. Get a MIME string. HTML pages typically return text/html.
  3. Reflects rendering. It is the type the browser is using for this document.
  4. Debug aid. Unexpected types can explain odd parsing or CSS behavior.

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

  1. Compare MIME types. Strict equality against "text/html".
  2. Boolean flag. isHtml is handy in shared helpers that may run in HTML or XML/SVG docs.
  3. Keep the raw value. Log contentType for support reports.
  4. Try-it pages. These demos are HTML, so the flag is usually true.

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 of document?"
// characterSet ≈ "which text encoding?"
Try It Yourself

How It Works

  1. MIME first. contentType says the document kind (text/html).
  2. Encoding second. characterSet says how bytes became text (UTF-8).
  3. Do not mix them. A page can be text/html with many encodings.
  4. Learn more. See the characterSet tutorial for encoding details.

Browser Support

Document.contentType is Baseline Widely available on MDN (since April 2018). It works across modern browsers for reading the rendered document MIME type.

✓ Baseline · Widely available

Document.contentType

Read-only MIME type string for the document currently being rendered — typically text/html.

99% Browser support
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
contentType Excellent

Bottom line: Safe for modern web apps. Remember: configure MIME types with HTTP headers on the server — <meta> tags do not update this property.

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.

Next: cookie

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

cookie 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