TrustedHTML: toJSON() method

Baseline 2026
Newly available

Since February 2026, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Note: This feature is available in Web Workers.

Phương thức toJSON() của giao diện TrustedHTML trả về biểu diễn JSON của dữ liệu được lưu trữ.

Cú pháp

js
toJSON()

Tham số

Không có.

Giá trị trả về

Một chuỗi chứa biểu diễn JSON của dữ liệu được lưu trữ.

Ví dụ

Hằng số escaped là đối tượng được tạo thông qua chính sách Trusted Types escapeHTMLPolicy. Phương thức toString() trả về chuỗi để chèn an toàn vào tài liệu.

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

const escaped = escapeHTMLPolicy.createHTML("<img src=x onerror=alert(1)>");
console.log(escaped.toJSON());

Thông số kỹ thuật

Thông số kỹ thuật
Trusted Types
# dom-trustedhtml-tojson

Tương thích trình duyệt