CKTIS / JavaScript
Element.innerHTML and text insertion security boundaries
Choose between parsing markup and inserting literal text. Learn how to prevent DOM XSS and what changes when Trusted Types are enforced.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
const markup = element.innerHTML;
element.innerHTML = htmlStringOrTrustedHTML;
const text = element.textContent;
element.textContent = plainText;
element.append(plainText);
element.insertAdjacentText("beforeend", plainText);
element.setHTML(htmlToSanitize); // Where supported.Reading Element.innerHTML serializes an element’s contents, not its outer tag. In an HTML document, writing it parses a fragment in the element’s context and normally replaces its existing children. For an HTML <template> element, the getter serializes the contents of template.content, and the setter replaces the children of that DocumentFragment, not the element’s own children. element.innerHTML += fragment still replaces nodes rather than appending to them: it reads the serialized contents and then parses the combined string again.
On an element, reading Node.textContent returns the combined text of its descendants, not their markup. Assigning a nonempty string replaces the children with one text node; assigning an empty string clears them. To keep existing children, use append(text) to add a string at the end or insertAdjacentText(position, text) to insert it at a chosen position relative to the element. None of these text operations interprets markup or character references in the string.
Do not pass untrusted strings to innerHTML: it is an HTML injection sink that can lead to DOM XSS. script elements inserted this way do not execute, but other mechanisms, such as event-handler attributes, can still run code. Display untrusted text in an ordinary element such as a div. Text insertion does not make the same data safe in a script, style, or URL context, or if you later parse it as HTML.
If you need rich HTML from an untrusted source, use setHTML() where supported, or use a vetted HTML sanitizer before insertion. Never fall back to raw innerHTML. When CSP enforces Trusted Types, assigning an ordinary string to innerHTML without a default policy throws TypeError. TrustedHTML does not guarantee sanitization: its safety depends on the policy that created it. These are web-platform APIs, not ECMAScript features; check support for the safeguards you use.
Examples
<!doctype html>
<meta charset="utf-8">
<title>innerHTML and textContent</title>
<script>
{
const sample = "<em>A & B</em>";
const parsed = document.createElement("div");
const literal = document.createElement("div");
parsed.innerHTML = sample;
literal.textContent = sample;
console.log(`innerHTML: ${parsed.childElementCount} | ${parsed.textContent}`);
console.log(`textContent: ${literal.childElementCount} | ${literal.textContent}`);
}
</script>Result
innerHTML: 1 | A & B textContent: 0 | <em>A & B</em>
Save the example as an HTML file, open it in a browser without Trusted Types enforcement, and check the browser console. The fixed string is harmless: parsing creates one `em` element and decodes the character reference to `&`. Text insertion preserves the whole string and creates no child element.
Related lessons
- JavaScript · Lesson 15 · Advanced · Version: 4