OBV
OBV

Reputation: 1259

Double quote escaping with Javascript function

JSON.stringify escapes double quotes. Is there an input such that the following code (without modification) does not result in the escape of the double quotes?

<script>
function test(s) {
document.write(JSON.stringify(s));
}
</script>


  <form action="" method="post" onsubmit="test(this.cmd.value); return false;">
  <input class="command" type="text" id="cmdbox" name="cmd" />
  </form>

Example input/output:

"test"

Expected:

"test"

Actual:

\"test\"

Upvotes: 0

Views: 303

Answers (1)

Bernie
Bernie

Reputation: 5055

Use to unescape the escaped string to get the expected result.

unescape(JSON.stringify(data))

Upvotes: 2

Related Questions