Skip to content

Instantly share code, notes, and snippets.

View JalmarazMartn's full-sized avatar

Jesús Almaraz Martín JalmarazMartn

View GitHub Profile
// 👍 ✔ originally posted on:
// https://dirask.com/posts/10Wwaj
// we can run this code online under above link ☘
//
<!doctype html>
<html>
<body>
<div>
<img id="image" style="border: 1px solid silver; width: 320px; height: 240px" />
@mathiasbynens
mathiasbynens / escape.js
Created June 26, 2019 07:29
Escaping JSON-stringified data for use as a JavaScript string literal
// This object contains a string value that in contains a single quote,
// a double quote, a backtick, and a backslash.
const data = { foo: `a'b"c\`d\\e` };
// Turn the data into its JSON-stringified form.
const json = JSON.stringify(data);
// Now, we want to insert the data into a script body as a JavaScript
// string literal per https://v8.dev/blog/cost-of-javascript-2019#json,
// escaping special characters like `"` in the data.