This :
<script type="text/javascript">
var a = new Array("</script>");
alert(a);
</script>
...doesn't work as expected (by me at least). The string "</script>"
is interpreted as the end of the script even though it's in quotes. Please see these jsfiddles :
The first declares the first element of the array in the normal way while the second declares it in a compact form. In both cases, browsers (Chrome, FF, IE) stop the script at "</script>"
and never get to the alert.
It behaves the same way whether using quotes or double quotes.
Is it expected ? Is there a workaround?
Edit: thanks all, I'll escape the backslash. Sorry I couldn't accept everyone's answer, I accepted the first one, but thanks to everyone.
The link provided by Juhana explains this behavior:
all the HTML is parsed before the text nodes in the element are passed to the JS engine,
</script>
gets no special treatment for being inside a JS string literal