stringifyForHtml
Serializes a value to compact JSON that is safe to inline in an HTML page, typically in a
<script type="application/json"> block or a script assignment.
<, > and & are written as \u003c, \u003e and \u0026, so the text can never close
the script element (</script>), open a comment (<!--) or be read as an entity. U+2028 and
U+2029, valid in JSON but line terminators in some script contexts, are escaped too. These
characters can only appear inside JSON strings, so the escaped text parses back to exactly the
same value with JSON.parse.
It throws like stringify does (a TypeError for undefined, a function, a symbol, a
BigInt or a circular structure).
Available since v3.1.5
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value | unknown | The value to serialize |
Returns
Section titled “Returns”string — Compact JSON text with no raw <, >, &, U+2028 or U+2029
Examples
Section titled “Examples”Inline data in a script block
Section titled “Inline data in a script block”A value containing can no longer close the element it is embedded in.
Same value after parsing
Section titled “Same value after parsing”The escapes are plain JSON escapes: JSON.parse gives back the original value.
