DOMParser: parseFromString() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The parseFromString() method of the DOMParser interface parses a string containing either HTML or XML, returning an HTMLDocument or an XMLDocument.

Note: The Document.parseHTMLUnsafe() static method provides an ergonomic alternative for parsing HTML strings into a Document.

Syntax

js
parseFromString(string, mimeType)

Parameters

string

The string to be parsed. It must contain either an HTML, xml, XHTML, or svg document.

mimeType

A string. This string determines whether the XML parser or the HTML parser is used to parse the string. Valid values are:

  • text/html
  • text/xml
  • application/xml
  • application/xhtml+xml
  • image/svg+xml

A value of text/html will invoke the HTML parser, and the method will return an HTMLDocument. Any