JavaScript Document createCDATASection() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.createCDATASection(data) creates a new CDATA section node containing a string and returns it. CDATA is an XML feature for raw text where < and & do not need escaping.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call document.createCDATASection. Start with this line.

JavaScript
document.createCDATASection(data)
CallWhat it does
document.createCDATASection(data)Primary signature.
document.createCDATASection(...)Call with the arguments your case needs.

Built into the browser. Call createCDATASection(...) on document — no import required.

Library declarations
JavaScript
document.createCDATASection(data)

Parameters

ParameterDescription
XML DocumentRequired. Caller must support CDATA (not HTML)
dataRequired. String content of the CDATA section

Return Value

Return ValueDescription
*A CDATASection node (not yet in the tree)

JavaScript Examples

Example 1 — Create CDATA and Serialize XML

Parse a tiny XML document, append a CDATA section, then serialize it.

JavaScript
const doc = new DOMParser().parseFromString("<xml></xml>", "application/xml");
const cdata = doc.createCDATASection("Some <CDATA> data & then some");
doc.querySelector("xml").appendChild(cdata);

console.log(new XMLSerializer().serializeToString(doc));
// <xml><![CDATA[Some <CDATA> data & then some]]></xml>
Try It Yourself

How It Works

  1. Parse XML. application/xml builds a real XML document (not HTML).
  2. Create CDATA. < and & stay literal inside the section.
  3. Append. The node must be attached before serialization shows it.
  4. Serialize. XMLSerializer prints the classic <![CDATA[…]]> wrapper.

Example 2 — HTML Documents Throw

The page document is HTML, so CDATA creation is rejected.

JavaScript
try {
  document.createCDATASection("hello");
} catch (err) {
  console.log(err.name); // "NotSupportedError"
}
Try It Yourself

How It Works

  1. Call on HTML. Normal web pages use an HTML document.
  2. Browser rejects it. CDATA is not part of the HTML DOM model.
  3. Catch the error. You get a DOMException named NotSupportedError.
  4. Fix. Use an XML document, or switch to createTextNode() for HTML text.

Example 3 — Forbidden ]]> Sequence

The CDATA closer cannot appear inside the data string.

JavaScript
const doc = new DOMParser().parseFromString("<root/>", "application/xml");

try {
  doc.createCDATASection("safe ]]> bad");
} catch (err) {
  console.log(err.name); // "InvalidCharacterError"
}
Try It Yourself

How It Works

  1. Start with XML. Parsing succeeds; the document supports CDATA.
  2. Pass forbidden text. ]]> is the CDATA end marker.
  3. Engine throws. Modern browsers report InvalidCharacterError.
  4. Rule. Validate or strip ]]> before calling — or use createTextNode().

Browser Support

Document.createCDATASection() is a browser DOM API marked Baseline Widely available on MDN. Remember: availability ≠ usable on HTML documents.

✓ Baseline Widely available

Document.createCDATASection()

Supported in Chrome, Firefox, Safari, Edge, Opera, and Internet Explorer. On HTML documents the call throws NotSupportedError.

Baseline Widely available
Google Chrome All modern versions
Yes
Microsoft Edge All modern versions
Yes
Mozilla Firefox All modern versions
Yes
Apple Safari All modern versions
Yes
Opera All modern versions
Yes
Internet Explorer IE 9+
Yes
createCDATASection() Excellent in browsers

Bottom line: Safe for XML workflows in browsers. Use DOMParser with application/xml. See the MDN createCDATASection() reference.

Frequently Asked Questions

It creates a new CDATA section node containing the given string and returns it. In XML, CDATA holds raw text so characters like < and & do not need escaping.
No. MDN marks Document.createCDATASection() as Baseline Widely available. It is not Deprecated, Experimental, or Non-standard.
No. HTML documents do not support CDATA sections. Calling it on an HTML document throws a DOMException (NOT_SUPPORTED_ERR / NotSupportedError). Use it on an XML document, for example one from DOMParser with application/xml.
The sequence ]]> ends a CDATA section in XML. Passing it inside the data throws InvalidCharacterError (historically NS_ERROR_DOM_INVALID_CHARACTER_ERR). Do not put unvalidated user input into createCDATASection().
For HTML documents, always. For many XML cases, createTextNode() is enough if you let the serializer escape special characters. Use createCDATASection() when you specifically need a CDATA section in XML output.
Node.CDATA_SECTION_NODE, which is 4. You can read the raw text with nodeValue or data / textContent.

Next: createComment()

Learn how to create DOM comment nodes with document.createComment().

createComment() 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