Document.parseHTML(input) is a static method that parses an HTML string, sanitizes it (XSS-safe), and returns a newDocument (MDN). Prefer it over parseHTMLUnsafe() unless you must allow unsafe markup.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call Document.parseHTML. Start with this line.
JavaScript
Document.parseHTML(input, options?)
Call
What it does
Document.parseHTML(input, options?)
Primary signature.
Document.parseHTML(...)
Call with the arguments your case needs.
Built into the browser. Call Document.parseHTML(...) on the Document constructor — no import required.
Library declarations
JavaScript
Document.parseHTML(input, options?)
Parameters
Parameter
Description
input
Required. HTML string to parse and sanitize
options.sanitizer
Optional. Sanitizer, SanitizerConfig, or "default"
Return Value
Return Value
Description
*
New XSS-safe Document
JavaScript Examples
Example 1 — Parse HTML and Strip Scripts
Default sanitizer removes XSS-unsafe markup such as <script>.
JavaScript
if (typeof Document.parseHTML !== "function") {
console.log("unsupported");
} else {
const html = "
Fall back to DOMParser. Baseline parsing without that sanitizer.
Return which path ran. Helps demos and debugging.
Production note. On the DOMParser path, add a real sanitizer (library or server).
Compatibility
Browser Support
Document.parseHTML() is Limited availability on MDN (not Baseline). Support includes Chrome/Edge 146+ and Firefox 148+; Safari currently lacks support. See the MDN parseHTML() reference.
✓ Limited availability
Document.parseHTML()
XSS-safe static HTML → Document. Feature-detect and provide a sanitizing fallback.
LimitedNot Baseline
Google Chrome146+
Yes
Microsoft Edge146+
Yes
Mozilla Firefox148+
Yes
OperaFollow Chromium
Partial
Apple SafariNot supported
No
Internet ExplorerNot supported
No
parseHTML()Partial
Bottom line: Use parseHTML when available. Elsewhere, DOMParser plus a sanitizer library (or server-side sanitization) is the practical fallback.
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.