Element.setHTMLUnsafe() parses HTML into a DocumentFragment, optionally filters it, then replaces the element’s subtree (MDN). It returns undefined. Prefer setHTML() when available — it always strips XSS-unsafe markup.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call el.setHTMLUnsafe. Start with this line.
JavaScript
element.setHTMLUnsafe(input)
Call
What it does
element.setHTMLUnsafe(input)
Primary signature.
el.setHTMLUnsafe(...)
Call with the arguments your case needs.
Built into the browser. Call setHTMLUnsafe(...) on an Element — no import required.
Library declarations
JavaScript
element.setHTMLUnsafe(input)
Parameters
Parameter
Description
input
Required. TrustedHTML or HTML string
options.sanitizer
Optional. Sanitizer, config, or "default"
Return Value
Return Value
Description
*
Always undefined
JavaScript Examples
Example 1 — Basic Inject (No Sanitizer)
With no options, HTML is injected without filtering (MDN).
JavaScript
const target = document.getElementById("target");
const html = "
Else use default sanitizer. Safer than no filter at all.
Last resort: text-only — avoid raw innerHTML for untrusted input.
Why “Unsafe”? This API does not force XSS-safe stripping.
Compatibility
Browser Support
Element.setHTMLUnsafe() is Baseline 2025 on MDN (newly available since September 2025). Feature-detect in older engines. Sanitizer options may have varying support.
✓ Baseline 2025
Element.setHTMLUnsafe()
HTML inject with optional sanitizer — Baseline newly available.
2025Baseline
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
setHTMLUnsafe()Baseline 2025
Bottom line: Detect typeof el.setHTMLUnsafe === "function". Prefer setHTML for untrusted HTML when available; treat this API as an injection sink.
Frequently Asked Questions
It parses HTML input into a DocumentFragment, optionally filters it, and replaces the element's subtree in the DOM (MDN). Unlike innerHTML, declarative shadow roots in the input are parsed.
No. MDN marks Element.setHTMLUnsafe() as Baseline 2025 (newly available since September 2025). It is not Deprecated, Experimental, or Non-standard.
undefined. There is no return value (MDN).
MDN: almost always, on browsers where setHTML is supported. setHTML always removes XSS-unsafe entities. Use setHTMLUnsafe only for edge cases like declarative shadow roots or when you must allow specific unsafe HTML.
No by default. MDN warns it is an injection sink. Prefer setHTML for untrusted HTML, or TrustedHTML with Trusted Types enforced. Next safest: setHTMLUnsafe with the default sanitizer.
Optional Sanitizer, SanitizerConfig, or the string "default". If omitted, no sanitizer runs and all HTML entities in the input are injected (MDN).