Learn all about the jQuery function jQuery.parseHTML().
jQuery.parseHTML uses native methods to convert the string to a set of DOM nodes, which can then be inserted into the document. These methods do render all trailing or leading text (even if that’s just whitespace). To prevent trailing/leading whitespace from being converted to text nodes you can pass the HTML string through
By default, the
context is the current
document if not specified or given as
undefined. If the HTML was to be used in another document such as an iframe, that frame’s document could be used.
Most jQuery APIs that accept HTML strings will run scripts that are included in the HTML.
jQuery.parseHTML does not run scripts in the parsed HTML unless
keepScripts is explicitly
true. However, it is still possible in most environments to execute scripts indirectly, for example via the
<img onerror> attribute. The caller should be aware of this and guard against it by cleaning or escaping any untrusted inputs from sources such as the URL or cookies. For future compatibility, callers should not depend on the ability to run any script content when
keepScripts is unspecified or