document.createCDATASection() is an instance method that creates a CDATA section node for XML documents (see MDN Document: createCDATASection()). Learn the MDN DOMParser example, why HTML pages throw NOT_SUPPORTED_ERR, the forbidden ]]> sequence, and when createTextNode() is enough — with five try-it labs.
01
Kind
Instance method
02
Arg
data string
03
Returns
CDATASection
04
XML only
not HTML
05
Avoid
]]> in data
06
Status
Baseline
Fundamentals
Introduction
In XML, a CDATA section wraps raw text so characters like < and & are treated literally. In serialized XML it looks like:
JavaScript
<script><![CDATA[ if (a < b && c) alert("ok"); ]]></script>
MDN: createCDATASection(data) creates that kind of node in memory. You typically append it to an XML element with appendChild(), then serialize with XMLSerializer.
⚠️
HTML pages do not support CDATA (MDN)
Calling document.createCDATASection() on the live HTML page’s document throws NOT_SUPPORTED_ERR. Parse XML first with DOMParser and use the returned document.
An instance method on a Document object — usually an XML document from DOMParser, not the HTML page document (MDN).
Parameter — data: string for the CDATA content (MDN).
Return value — a CDATASection node (MDN).
nodeType — Node.CDATA_SECTION_NODE (4).
HTML — throws NOT_SUPPORTED_ERR (MDN).
Forbidden — data must not contain the literal ]]> (MDN).
Alternative — createTextNode() often suffices (MDN).
Foundation
📝 Syntax
General form of Document.createCDATASection (MDN):
JavaScript
createCDATASection(data)
Parameters
data — string containing the CDATA content (MDN).
Return value
A CDATA Section node (MDN).
MDN example (XML document)
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>
Compare
⚖️ createCDATASection vs related node creators
API
Returns
Document type
Notes
createCDATASection(data)
CDATASection
XML only
Raw text, no ]]>
createTextNode(data)
Text
HTML + XML
MDN alternative
createComment(data)
Comment
HTML + XML
<!-- --> nodes
createElement(tag)
Element
HTML + XML
Elements, not text
Cheat Sheet
⚡ Quick Reference
Goal
Code
Parse XML doc
new DOMParser().parseFromString(xml, "application/xml")
Example 1 — MDN: parse XML, create CDATA, serialize
Official MDN workflow with DOMParser and XMLSerializer.
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>
MDN: unescaped user data is unsafe unless you validate it does not contain ]]>.
Example 5 — Alternative: createTextNode() (MDN)
When you do not need a CDATA wrapper, a text node is often enough.
JavaScript
const doc = new DOMParser().parseFromString("<msg/>", "application/xml");
const root = doc.documentElement;
// Text node — works in HTML and XML
const text = doc.createTextNode("Hello & welcome");
root.appendChild(text);
console.log(new XMLSerializer().serializeToString(doc));
// Text is escaped in XML output: Hello & welcome
Document.createCDATASection() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.createCDATASection()
Creates CDATA Section nodes in XML documents — widely supported.
BaselineWidely available
Google ChromeSupported (XML)
Yes
Mozilla FirefoxSupported (XML)
Yes
Apple SafariSupported (XML)
Yes
Microsoft EdgeSupported (XML)
Yes
OperaSupported (XML)
Yes
Internet ExplorerSupported (legacy XML)
Yes
createCDATASection()Wide
Bottom line: Supported in major browsers for XML documents. Not available on HTML page documents — use DOMParser first.
Wrap Up
Conclusion
document.createCDATASection(data) creates a CDATA section node for XML documents. Use it when you need literal character data in serialized XML. On HTML page documents it throws; on XML, never pass ]]> inside data. Often, createTextNode() is the simpler choice.
Consider createTextNode() when escaping is fine (MDN)
Check nodeType === Node.CDATA_SECTION_NODE when debugging
❌ Don’t
Call on the live HTML document (MDN)
Embed raw user input without checking for ]]>
Expect CDATA in HTML DOM output
Use CDATA when a text node meets your needs
Confuse with HTML comments (createComment)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about createCDATASection()
XML-only raw text nodes.
5
Core concepts
📝01
Returns
CDATASection
node
📄02
Scope
XML only
MDN
⚠️03
Ban
]]> in data
MDN
⚡04
Alt
createTextNode
often
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.createCDATASection() creates a new CDATA section node and returns it. CDATA sections hold raw text in XML where special characters like < and & do not need escaping.
No. MDN marks Document.createCDATASection() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No (MDN). HTML documents do not support CDATA sections. Calling createCDATASection() on an HTML document throws NOT_SUPPORTED_ERR. Use it on XML documents — for example one parsed with DOMParser and application/xml.
MDN: a string parameter. The closing CDATA sequence ]]> must NOT appear inside the data — that throws NS_ERROR_DOM_INVALID_CHARACTER_ERR. Unescaped user input is unsafe unless you validate for ]]>.
MDN notes createTextNode() can often be used in its place. For HTML documents, use createTextNode() or textContent. CDATA is an XML-specific construct.
Node.CDATA_SECTION_NODE (4). You can read nodeValue or textContent for the raw character data inside the section.
Did you know?
MDN’s official example never touches the page’s HTML document — it creates a separate XML document with DOMParser, adds CDATA there, and prints the result with XMLSerializer. That pattern is the safe way to practice this API in the browser.