GeezerVison is showing developer.mozilla.org — TrustedScriptURL: toJSON() method - Web APIs | MDN
TrustedScriptURL: 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.
This example creates a TrustedScriptURL object. Calling toJSON() directly returns its stored string value.
js
const sanitized = scriptPolicy.createScriptURL(
"https://example.com/my-script.js",
);
const json = sanitized.toJSON();
console.log(json); // Same value as sanitized.toString()
console.log(typeof json); // "string"
The TrustedScriptURL object from the previous example is serialized using JSON.stringify(), which calls the toJSON() method automatically.
js
console.log(JSON.stringify(sanitized));
This would log the following JSON string, including the surrounding double quotes, which are part of the string's content:
json
"https://example.com/my-script.js"