Subject: | Escape Slashes / as in </script> |
Date: | Tue, 14 Apr 2015 16:38:25 +1000 |
To: | bug-JSON [...] rt.cpan.org |
From: | Maikel Linke <maikel [...] predikkta.com> |
Hello there,
I just ran into the problem that a string contained "</script>". That
string was in a data structure converted to JSON and then inserted into
JavaScript embedded in HTML. And that's a problem for browsers:
<html>
<script>
var greeting = "hello <script>kiddies</script>";
</script>
</html>
The browser will take the first </script> to end the HTML element and
won't find valid JavaScript in there. That's why slashes can be escaped
with backslashes: "<\/script>".
It is valid and safe to do that in general:
encode_json(["</script>"])
would become:
[<\/script>]
Did I overlook something or is there an option to encode slashes?
Cheers,
maikel