Document.parseHTML() is a static method that parses and sanitizes an HTML string into a new Document that is XSS-safe (MDN). Learn default vs custom Sanitizer options, how it differs from parseHTMLUnsafe() and DOMParser, feature detection, and five examples with try-it labs.
01
Kind
Static method
02
Returns
Document
03
Args
input, options
04
Security
XSS-safe
05
MIME
text/html
06
Status
Limited availability
Fundamentals
Introduction
Sometimes you need a whole Document from an HTML string—not just inject into one element. Older code often used DOMParser.parseFromString(html, "text/html"), which parses without the built-in XSS sanitizer that parseHTML() provides.
MDN: Document.parseHTML(input, options) parses and sanitizes the string and returns a new Document. The result has content type "text/html", character set UTF-8, and URL "about:blank".
💡
Prefer the safe API (MDN)
Use Document.parseHTML() instead of Document.parseHTMLUnsafe() unless you specifically need unsafe elements and attributes. XSS-unsafe entities are always removed by parseHTML(), even if a custom sanitizer would allow them.
input — string of HTML to sanitize and parse into a Document (MDN).
options (optional) — object with sanitizer: a Sanitizer, SanitizerConfig, or the string "default". If omitted, the default sanitizer is used (MDN).
Return value
A Document (MDN).
Exceptions
TypeError — invalid SanitizerConfig (e.g. both allowed and removed settings), a string other than "default", or a value that is not a Sanitizer / SanitizerConfig / string (MDN).
Document.parseHTML() has limited availability on MDN (HTML Sanitizer API / HTML spec). Support is emerging in Chromium and Firefox; Safari may lag. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Limited availability
Document.parseHTML()
XSS-safe static HTML → Document — feature-detect and provide a fallback.
GrowingLimited availability
Google ChromeEmerging / recent versions · check
Partial
Microsoft EdgeFollow Chromium support
Partial
Mozilla FirefoxEmerging / recent versions · check
Partial
Apple SafariLimited or unavailable · check version
Partial
OperaFollow Chromium support
Partial
Internet ExplorerNot supported
No
parseHTML()Partial
Bottom line: Feature-detect Document.parseHTML. Fallback to DOMParser plus a sanitizer library (e.g. DOMPurify) or server-side sanitization where support is missing.
Wrap Up
Conclusion
Document.parseHTML() is the XSS-safe static way to turn an HTML string into a new Document. MDN prefers it over parseHTMLUnsafe() for untrusted markup. Support is still limited, so always feature-detect.
Call Document.parseHTML() for untrusted HTML strings (MDN)
Feature-detect with typeof Document.parseHTML === "function"
Reuse a Sanitizer instance for repeated configs (MDN)
Use setHTML() when injecting into one existing element
Provide a sanitizing fallback when support is missing
❌ Don’t
Use parseHTMLUnsafe() for user content (MDN)
Assume Baseline support in all browsers yet
Skip sanitization when falling back to DOMParser
Confuse static parseHTML with instance setHTML
Rely on Trusted Types to validate this call (MDN: not gated that way)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about parseHTML()
Static, XSS-safe HTML → Document — feature-detect first.
5
Core concepts
📝01
Kind
static
Document.
🛡02
Security
XSS-safe
MDN
📄03
Returns
Document
text/html
✓04
Prefer
over Unsafe
MDN
🔍05
Status
limited
Detect
❓ Frequently Asked Questions
It is a static method that parses and sanitizes an HTML string and returns a new Document instance that is XSS-safe (MDN).
No. MDN marks Document.parseHTML() as Limited availability (not Baseline). It is not Deprecated, Experimental, or Non-standard; it is defined in the HTML specification.
MDN: content type "text/html", character set UTF-8, and URL "about:blank".
MDN: only when you specifically need to allow unsafe elements and attributes. Prefer Document.parseHTML() for untrusted HTML.
DOMParser.parseFromString() parses HTML or XML without the built-in XSS sanitizer that parseHTML() applies. parseHTML() always removes XSS-unsafe entities (MDN).
MDN: because this method always sanitizes XSS-unsafe entities, it is not secured or validated using the Trusted Types API.
Did you know?
MDN notes that if the HTML string defines more than one declarative shadow root on the same shadow host, only the firstShadowRoot is created—later declarations are parsed as elements inside that shadow root.