Element.setHTML() is an instance method that sanitizes and injects HTML into an element using the HTML Sanitizer API. Learn MDN’s script-stripping demo, default vs custom Sanitizer options, why it beats innerHTML for user content, mutation XSS (mXSS) rules, and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Returns
undefined
03
Args
input, options
04
Security
XSS-safe
05
Sanitizer
Default/custom
06
Status
Limited availability
Fundamentals
Introduction
Setting HTML from a string is common in rich editors, comment widgets, and dynamic UIs. The classic approach — element.innerHTML = userHtml — is dangerous with untrusted input because it can run scripts or event handlers.
element.setHTML(input, options) parses the string, sanitizes it with the HTML Sanitizer API, and inserts the result as a subtree of the element (MDN). MDN recommends it as a drop-in replacement for innerHTML when the HTML comes from users.
💡
Always removed (MDN)
Even with a permissive custom sanitizer, setHTML() always strips XSS-unsafe elements (script, iframe, etc.) and event-handler attributes like onclick.
Examples follow MDN Element.setHTML() patterns. Use View Output or Try It Yourself for each case. Feature-detect setHTML and Sanitizer in supporting browsers.
📚 Getting Started
MDN’s basic demo—strip <script> from untrusted HTML.
Example 1 — Strip <script> (MDN)
Sanitize and inject HTML; unsafe tags are removed automatically.
Element.setHTML() has limited availability on MDN (HTML Sanitizer API). It is growing in Chromium-based browsers and Firefox. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Limited availability
Element.setHTML()
XSS-safe HTML injection with built-in sanitization — prefer over innerHTML for user content.
GrowingLimited availability
Google ChromeSupported · recent Chromium versions
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · recent versions
Partial
Apple SafariLimited or unavailable · check version
Partial
OperaFollow Chromium support
Partial
Internet ExplorerNot supported
No
setHTML()Partial
Bottom line: Feature-detect setHTML and Sanitizer before use. Provide innerHTML + DOMPurify or server-side sanitization as fallback where needed.
Wrap Up
Conclusion
Element.setHTML() sanitizes and injects HTML strings safely. MDN recommends it over innerHTML for user-provided markup. Returns undefined and always strips XSS-unsafe content.
Reuse a Sanitizer instance for repeated configs (MDN)
Re-inject serialized HTML only via setHTML()
Provide a fallback sanitizer library where unsupported
❌ Don’t
Assign user strings to innerHTML
Re-parse innerHTML output into another element
Expect script to survive even if allow-listed
Use setHTMLUnsafe() for user content
Assume all browsers support the Sanitizer API yet
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.setHTML()
Sanitize and inject — the safe way to set HTML from strings.
5
Core concepts
📝01
Returns
undefined
API
🛡️02
Security
XSS-safe
Sanitize
⚙️03
Sanitizer
default/custom
Config
⚠️04
mXSS
no innerHTML
Caution
🌐05
Support
limited
Detect
❓ Frequently Asked Questions
It parses and sanitizes a string of HTML, then inserts it into the element as a DOM subtree. MDN recommends it as an XSS-safe replacement for innerHTML when setting user-provided HTML.
No. MDN marks it as Limited availability — not Baseline in all major browsers yet. It is not Deprecated, Experimental, or Non-standard; it is in the HTML specification.
undefined. There is no return value (MDN).
input (HTML string) and an optional options object with sanitizer — a Sanitizer, SanitizerConfig, or the string "default" (MDN).
innerHTML parses HTML without built-in sanitization. setHTML() always removes XSS-unsafe elements and attributes (script, onclick, etc.) even if a custom sanitizer would allow them (MDN).
MDN: only when you specifically need to allow unsafe elements and attributes. For untrusted user HTML, prefer setHTML().
Did you know?
MDN’s setHTML() demo shows that even when you allow script in a custom Sanitizer, the method still removes it — XSS-unsafe entities are always stripped on top of your configuration.