Chrome and Edge add new methods for inserting and streaming HTML
Chrome and Edge are adding a new set of methods for inserting HTML into an existing element and for streaming HTML directly into the [DOM](1). The new methods put the action in the name: appendHTML() adds content to the end of an element, prependHTML() adds it to the start, and setHTML() replaces the existing content. There are six methods for inserting or replacing HTML in total.
By default, the regular methods run the browser's built-in sanitizer, which removes unsafe content such as script elements. Developers can pass a custom [sanitizer](2) that allows only chosen elements. The Unsafe variants, such as setHTMLUnsafe(), skip the default sanitizer, and their runScripts option is false by default.
Streaming variants such as streamAppendHTML() insert HTML as it arrives instead of waiting for the full response. Support is limited: the methods are in recent Chromium releases but not in all browsers, so support should be checked first. Chrome has released an [html-setters-polyfill](3) that gives the same method shape in other browsers, but it waits for all content before inserting it, so it does not provide native streaming.
Product Google Chrome google.com
Google's web browser, built for speed, security and customization; it is the browser the entry's new HTML methods are added to.
Read the original amitmerchant.comDiscussion
Sources
-
Amit Merchant
JavaScript Gets a New Set of HTML Insertion APIs
Terms
- DOM — Document Object Model: the browser's tree of page elements that JavaScript can read and change.
- sanitizer — A filter that removes unsafe parts of HTML, such as scripts, before the HTML is inserted into a page.
- polyfill — Code that adds a newer browser feature to browsers that do not support it yet.





