Back to Content

TrustedHTML: toString() method

files/en-us/web/api/trustedhtml/tostring/index.md

latest825 B
Original Source

{{APIRef("Trusted Types API")}}{{AvailableInWorkers}}

The toString() method of the {{domxref("TrustedHTML")}} interface returns a string which may safely inserted into an injection sink.

Syntax

js-nolint
toString()

Parameters

None.

Return value

A string containing the sanitized HTML.

Examples

The constant escaped is an object created via the Trusted Types policy escapeHTMLPolicy. The toString() method returns a string to safely insert into a document.

js
const escapeHTMLPolicy = trustedTypes.createPolicy("myEscapePolicy", {
  createHTML: (string) => string.replace(/</g, "&lt;"),
});

const escaped = escapeHTMLPolicy.createHTML("");
console.log(escaped.toString());

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}