On the one hand if I have
\';
console.log(s);
the browser will terminate the
The following characters could interfere with an HTML or Javascript parser and should be escaped in string literals: <, >, ", ', \,
and &
.
In a script block using the escape character, as you found out, works. The concatenation method ('
) can be hard to read.
var s = 'Hello <\/script>';
For inline Javascript in HTML, you can use entities:
click me
Demo: http://jsfiddle.net/ThinkingStiff/67RZH/
The method that works in both blocks and inline Javascript is
\uxxxx
, where xxxx
is the hexadecimal character code.
<
- \u003c
>
- \u003e
"
- \u0022
'
- \u0027
\
- \u005c
&
- \u0026
Demo: http://jsfiddle.net/ThinkingStiff/Vz8n7/
HTML:
click me